Using timeout to keep productive usage in check

Take a case in which we are trying to access a response which is taking too much time. If we don't want to get the process moving forward and give out an exception if it exceeds a specific amount of time, we can use the parameter timeout.

When we use the timeout parameter, we are telling Requests not to wait for a response after some specific time period. If we use timeout, it's not equivalent to defining a time limit on the whole response download. It's a good practice to raise an exception if no bytes have been acknowledged on the underlying socket for the stated timeout in seconds.

>>> requests.get('http://google.com', timeout=0.03)
---------------------------------------------------------------------------
Timeout                                   Traceback (most recent call last)
…….
……..
Timeout: HTTPConnectionPool(host='google.com', port=80): Read timed out. (read timeout=0.03)

In this example we have specified the timeout value as 0.03 in which the timeout has been exceeded to bring us the response and so it resulted us the timeout exception. The timeout may occur in two different cases:

  • The request getting timed out while attempting to connect to the server that is in a remote place.
  • The request getting timed out if the server did not send the whole response in the allocated time period.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.128.145