Java 使用Python获取API-超时问题
Q) 我想使用python调用一个GETAPI,这需要时间才能得到响应。 我不需要等待响应,我只想发送一个请求并关闭线程。请帮我解决这个问题。在这种情况下我该怎么办Java 使用Python获取API-超时问题,java,python,api,async-await,Java,Python,Api,Async Await,Q) 我想使用python调用一个GETAPI,这需要时间才能得到响应。 我不需要等待响应,我只想发送一个请求并关闭线程。请帮我解决这个问题。在这种情况下我该怎么办 def call_oms(in_params, thread_ts): url_list = { "static": "http://ptw01am1ap001:2003/execute" } executionId = str(random()) # real data o
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)
Mayrequests.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))
我认为您应该使用requests.post()而不是requests.get()。它实际上是一个get API,我不能执行requests.post()