Force a maximum time to download image from URL

Miguel Andrade :

I'm trying to implement a method which tries to make a few attempts to download an image from url. To do so, I'm using requests lib. An example of my code is:

while attempts < nmr_attempts:
        try:
            attempts += 1
            response = requests.get(self.basis_url, params=query_params, timeout=response_timeout)
        except Exception as e:
            pass

Each attempt can't spend more than "response_timeout" making the request. However It seems that the timeout variable is not doing anything since it does not respect the times given by myself.

How can I limit the max blocking time at response.get() call. Thanks in advance

Ali Cirik :

Can you try following (get rid of try-except block) and see if it helps? except Exception is probably suppressing the exception that requests.get throws.

while attempts < nmr_attempts:
    response = requests.get(self.basis_url, params=query_params, timeout=response_timeout)

Or with your original code, you can catch requests.exceptions.ReadTimeout exception. Such as:

while attempts < nmr_attempts:
    try:
        attempts += 1
        response = requests.get(self.basis_url, params=query_params, timeout=response_timeout)
    except requests.exceptions.ReadTimeout as e:
        do_something()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=12080&siteId=1