Timeout in requests.get

Hi,
I was working with “requests.get” method to call an api. I have observed some instances where even after setting timeout thresholds in this method (read,connect or total), the requests sometimes doesn’t timeout within the mentioned range.
Tried setting timeout parameter in requests.get, also tried setting connect and read timeouts separately, but still at times i observe delays.
I even tried the same with urllib3, but even this didn’t help.

In all the instances mentioned above, i was trying to call a flask+uwsgi api built in python only and i could see that the logs being generated have siginficantly shorter response time but when i track the actual time when i received the response, i can see significant delay. The api is hosted on a gcp cloud vm and i am accessing it using a vpn.

Is there any way to implement strict timeouts on requests?? Am i missing something?

Advanced Usage — Requests 2.32.3 documentation :slight_smile:

hmm, my bad have not read entirely before submit :wink:

when you say doesn’t timeout within mentioned range, can you elaborate ?

does it take longer ? how much and what timeout value(s) are you using ?

One weird thing about timeouts:

The connect timeout applies to each connection attempt to an IP address. If multiple addresses exist for a domain name, the underlying urllib3 will try each address sequentially until one successfully connects. This may lead to an effective total connection timeout multiple times longer than the specified time, e.g. an unresponsive server having both IPv4 and IPv6 addresses will have its perceived timeout doubled, so take that into account when setting the connection timeout.

From Advanced Usage — Requests 2.32.3 documentation

So it’s quite possible that you have multiple underlying requests.