We have usages of the requests library littered throughout our project. Recently we came across a bug in one of our destinations where it froze mid transaction, and decided to just hold the connection open.
Naturally, our application followed suit.
Is there a environment variable, or some other way to set the timeout? Even if it's significant (say, 30 seconds) it should be enough to stop the entire works from stopping because of one service. If possible, it should be global so that I don't have to find every single use, and so that people can't forget to add it in the future.
Unfortunately, looking at the code, there is no possibility to set a global default value. I was kinda surprised by that, as I would expect that to be quite common use case. If you start a feature request, please let me know (e.g. in comments to this post).
The reason for that is that methods like get(...), post(...), etc are all just thin wrappers over Session.request(...) method (requests.get(...) creates new one-shot session, just for a single request). That method takes timeout as argument, and does not inspect Session internals for a value if there is no timeout argument, so you always have to put it there manually, like 2ps proposed in his answer.
Sources:
Revised on master on 31.08.2020. Line numbers have changed, but methods stayed the same. The answer stays the same.
requests/__init__.py - import API to package scope, to provide requests.get(...)-like utilities
requests.api - API module that is imported in point above; usess one-shot sessions
requests.sessions - Session implementation
- line 337 starts
Session class
- line 463 starts
request(...) method
- line 526 actually uses
timeout parameter
- line 534 start
get(...) method
PS. See this pull request. Disclaimer: it's mine.
所有评论(0)