使用多进程的时候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))
博客内容提到,使用requests库进行多进程请求时,必须设置超时时间,否则程序可能会因服务器无响应而卡死。requests的timeout参数并不限制整个响应下载时间,而是针对连接建立后等待数据的时间。如果在指定时间内未收到任何数据,就会触发超时异常。timeout可以设置为单个数值或元组,分别用于连接和读取超时。了解这一点对于优化网络请求的性能至关重要。
2592

被折叠的 条评论
为什么被折叠?



