Requests is great. But if you’ve ever used it you may have encountered a problem with your app hanging for seemingly no reason.

(If you haven’t encountered that, then you will at some point, and it may well cause you serious pain)

The cause is very simple, yet non obvious, and it lies hidden at the bottom of the quickstart documentation:

You can tell Requests to stop waiting for a response after a given number of seconds with the timeout parameter. Nearly all production code should use this parameter in nearly all requests. Failure to do so can cause your program to hang indefinitely

Yes, the default timeout for a request is infinity

I’m sure there is some logic behind this, but really this should be at the top of the page in bright red flashing letters. So here it is in bright red flashing letters

default-timeout-is-infinity

To avoid problems, then, we should set an appropriate timeout value on all requests

connect_timeout=31 # number of seconds to wait for connection to be established
read_timeout=5 # maximum number of seconds between data reads

r = requests.get('https://github.com', timeout=(connect_timeout, read_timeout))