python requests 长时间卡死 无反应

使用多进程的时候requests一定要设置超时时间不然,程序会卡死在进程里

requests文档上有写,timeout is not a time limit on the entire response download; rather, an exception is raised if the server has not issued a response for timeout seconds (more precisely, if no bytes have been received on the underlying socket for timeout seconds). If no timeout is specified explicitly, requests do not time out.

大概的意思就是说requests请求上设置的timeout不是整个响应下载的时间限制,这里的timeout应该理解为与服务器连接后接收数据的时间,如果超过规定的时间则为超时。这与我之前的理解有很大不同。

timeout可以传一个浮点数,也可以传一个元组

如果你设置了一个单一的值作为 timeout,这一 timeout 值将会用作 connect(连接超时) 和 read (读取超时)二者的 timeout

requests.post(url, headers=headers,data=info_json, timeout=(5,10))


 

猜你喜欢

转载自blog.csdn.net/FishBean/article/details/124920742