requests.get()(Python,请求模块)是否暂停脚本直到响应到达?

Does requests.get() (Python, Requests module) pause a script until the response arrives?

这个问题是关于 Python 的请求模块的。

我正在使用 requests.get() 和 运行 解决一个问题,我超出了我联系的服务器允许我的请求速率限制。我在每个 requests.get() 之后设置了一个 time.sleep() 到一个更大的数量级。代码是这样的:

        for url in url_list:
            success = False
            response = requests.get(url) 
            while not success:
                time.sleep(1/250)
                if str(response) == "<Response [200]>":
                    time.sleep(1/250) # Wait Xs between API call      
                    do_stuff(response.text)
                    success = True

                else:
                    print(response)
                    print(response.headers)
                    time.sleep(3)
                    response = requests.get(url)

我一直在运行 提高我的速率,即每秒 300 个请求,而我应该最多每秒发送 125 个请求。在响应到达之前,请求不会暂停脚本吗?我不确定。我如何确保发送的请求不会超过我的费率?

requests.get 函数是阻塞调用。它会等到响应到达,然后程序的其余部分才会执行。如果您希望能够做其他事情,您可能需要查看 asyncio or multiprocessing 模块。