4

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

Miguel Andrade
  • 346
  • 3
  • 13
  • What is the value you set for response_timeout? – Ali Cirik Feb 03 '20 at 16:01
  • The value is 1.5 seconds. But if i specify 0.1 seconds the time of response its exactly the same. What I want is throw an exception whenever the the timeout is reached. Independently, if the request is completed or not. – Miguel Andrade Feb 03 '20 at 16:19

1 Answers1

3

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()
Ali Cirik
  • 1,475
  • 13
  • 21
  • But I want to catch the exception in case of timeout. I'll try to explain better: Imagine that you're making requests to an invalid URL. What I want is don't wait more than response timeout until receive the response from server. In my case this response brings an image in it content. – Miguel Andrade Feb 03 '20 at 17:41
  • @MiguelAndrade I updated my answer. You can catch requests.exceptions.ReadTimeout instead of the generic Exception. – Ali Cirik Feb 03 '20 at 17:55
  • It throws me another exceptions. I catch the requests.exceptions.ConnectTimeout but It also does not work. I've printend the time difference between the time that the request is made and the exception is throwed up and it is always 1 second. Independently of timeout. – Miguel Andrade Feb 03 '20 at 18:32