首页 > 解决方案 > Python请求无法识别来自服务器对高延迟请求的响应

问题描述

我正在尝试测试请求模块以触发需要很长时间才能运行的云中的服务(例如 Google Cloud Functions/Cloud run)。

我创建了一个测试服务,它在 45 分钟后提供一个 Json 响应对象。我可以在云运行日志中看到它工作正常并以 200 状态结束。

@app.route("/")
def test_high_latency_request():

    i=0
    while i < 9:
        time.sleep(300)
        i+=1
        print(str(i*5) + " mins have passed")

    response_json = {
        "asgfd":"dagdfs",
        "fgasdg":"gsadgs"
    }

    return response_json, 200

if __name__=="__main__":
    app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))

但是,即使响应可用,以下任何一个请求都不会收到响应,函数会无限期地继续运行,或者直到达到指定的超时时间,即使响应可用,从 Cloud Run 日志中可以看出。

requests.get("https://high-latency-conn-test-hyk3kw2bxq-nw.a.run.app",stream=True)
requests.get("https://high-latency-conn-test-hyk3kw2bxq-nw.a.run.app",timeout=(3600,3600))

我也尝试过启动 requests.Session() 实例,但这也无济于事。

如何使用此模块处理高延迟请求?或者我应该使用不同的库。

标签: python-requests

解决方案


推荐阅读