【问题标题】:Get API using Python - Timeout issue使用 Python 获取 API - 超时问题
【发布时间】:2020-05-15 11:12:31
【问题描述】:

Q) 我想使用 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.post() 而不是 requests.get()
  • 它实际上是一个GET API,我不能做requests.post()

标签: java python api async-await


【解决方案1】:

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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-31
    • 2022-11-02
    • 2022-08-04
    • 2021-11-12
    • 2017-03-22
    • 2017-09-20
    • 2018-06-02
    • 2010-10-11
    相关资源
    最近更新 更多