问)我想使用 python 调用 GET API,这需要时间才能获得响应。 我不需要等待响应,我只想发送请求并关闭线程。请帮我解决这个问题。这种情况我该怎么办?
def call_oms(in_params, thread_ts):
url_list = {
"static": "http://ptw01am1ap001:2003/execute"
}
executionId = str(random())
# real data
order_id =in_params["order_id"]
fulfillment = in_params["fulfillment"]
# env = in_params["env"]
# mocking data
# order_id = "CX01-1043562345"
# fulfillment = "shiptohome"
env = "static"
print("Consumming API -- In_params")
print(order_id)
print(fulfillment)
url = url_list[env] + "?orderNumber=" + order_id + "&testCaseName="+fulfillment + "&executionId="+ thread_ts
print(url) - This is printing the url with the parameters we send this to message
r = requests.get(url)
print(r.status_code)
最佳答案
requests.get(url, timeout=0)
可以帮助您吗?
添加:
import asyncio
import aiohttp
from random import random
def call_oms(in_params, thread_ts):
async def get(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
await response.read()
url_list = {
"static": "http://ptw01am1ap001:2003/execute"
}
executionId = str(random())
# real data
order_id =in_params["order_id"]
fulfillment = in_params["fulfillment"]
# env = in_params["env"]
# mocking data
# order_id = "CX01-1043562345"
# fulfillment = "shiptohome"
env = "static"
print("Consumming API -- In_params")
print(order_id)
print(fulfillment)
url = url_list[env] + "?orderNumber=" + order_id + "&testCaseName="+fulfillment + "&executionId="+ thread_ts
print(url)
courutine=get(url)
loop = asyncio.get_event_loop()
results = loop.run_until_complete(asyncio.gather(courutine))
关于java - 使用 Python 获取 API - 超时问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59975832/