Home>Article>Backend Development> What does request timeout mean?
Timeout
To prevent the server from failing to respond in time, most requests sent to external servers should carry the timeout parameter. By default, requests will not automatically timeout unless a timeout value is explicitly specified. Without timeout, your code might hang for several minutes or longer.
Connection timeout refers to the number of seconds Request will wait when your client realizes a connection to the remote machine port (corresponding to connect()). A good practice is to set the connection timeout to a value slightly larger than a multiple of 3, since the default size of the TCP packet retransmission window is 3.
In the crawler proxy area, we often encounter the problem of request timeout. The code is stuck there, no error is reported, and there is no response to the requests request.
Related recommendations: "Python Video Tutorial"
The usual processing is to add timeout to the requests.get() statement to limit the request time.
req = requests.get(url, headers=headers, proxies=proxies, timeout=5)
If you find that the long-term non-response problem still exists after setting timeout=5, you can refine the parameters in timeout
After making the following modifications, the problem will disappear
req = requests.get(url, headers=headers, proxies=proxies, timeout=(3,7))
timeout passes in a tuple (3,7). The first digit is the timeout time of the connect connection, and the second digit is the timeout time of read reading.
If encountered during the use of timeout ValueError error message (similar to:ValueError: Timeout value connect was Timeout(connect=2.0, read=2.0, total=None), but it must be an int or float.), this is python-requests version A bug in version 2.4.3-4 requires us to update requests
pip install -U requests
The above is the detailed content of What does request timeout mean?. For more information, please follow other related articles on the PHP Chinese website!