New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add timeout (and verify) options to requests call #2
Comments
I'm not seeing either of these arguments in the docs: http://docs.python-requests.org/en/latest/api/#requests.Request. Remember that you can always pass for resp in Requests().swarm([ Request('POST', url='http://www.google.com:81', any_other_args=...), ... ]):
print(resp.text) |
Yea indeed, I noticed that after I wrote. I've been using the timeout/verify args in But looking at the actual implementation of that |
Hmm I see. I suppose you could duplicate this behaviour by: requests = Requests()
requests.session.verify = False
requests.session.timeout = 30
for resp in requests.swarm([ ... ]):
print(resp.text) Looking through the requests code, I'm having difficulty figuring out where In either case, I'm hesitant to override requests' defaults, especially when it's easily work-aroundable. |
No I wasn't suggesting to override their defaults, but maybe add such parameters to your |
Hey again, it's used in https://github.com/kennethreitz/requests/blob/fe4c4f146124d7fba2e680581a5d6b9d98e3fdf8/requests/sessions.py#L522 - unfortunately, it expects it to be one of the kwargs in |
hey, any further thoughts on this? |
Sorry I let this slide; let me circle back and think about it again; I'll have something by the end of the day. |
Ok let's split this into The only usage I see is (https://github.com/kennethreitz/requests/blob/fe4c4f146124d7fba2e680581a5d6b9d98e3fdf8/requests/sessions.py#L570), which in turn has only one usage: a recursive call to the first method (https://github.com/kennethreitz/requests/blob/fe4c4f146124d7fba2e680581a5d6b9d98e3fdf8/requests/sessions.py#L165). If you can show me that it actually gets used somewhere, I'll open a ticket in the Requests project to have |
Hmm, odd, I'll test verify like that next week. As for timeout, it does indeed look odd, but I can tell for sure that setting it has an effect, so it has to be used somewhere... hm, need to find where |
I'll just want to second this request: an easy way to set a timeout on the requests that go out. I'll have a look at the code to see where it's set. In the requests API it looks like Requests are just data about where the request will go, but timeout is a property of making the request with .get(). |
@arski Ahh it's a double-bluff. Although timeout is pulled out of kwargs, it's still passed along as kwargs (https://github.com/kennethreitz/requests/blob/master/requests/sessions.py#L555) to the transport adapter. I've opened up a ticket (https://github.com/kennethreitz/requests/issues/2011) in requests to have timeout added to the Session, but it was shot down. According to the BDFL in https://github.com/kennethreitz/requests/issues/1130, the "correct" way to implement timeouts over a set of requests is to overwrite the transport adapter. I'm very hesitant about putting such code in simple-requests, as it would require 100 lines of copy&paste from https://github.com/kennethreitz/requests/blob/master/requests/adapters.py#L294. |
Maybe you could just expose the adapter interface to us, so we can do the copy-pasting on our side instead? |
First of all, confirming that your approach for As for |
Ok here's the recipe to make from simple_requests import Requests
requests = Requests()
# No default timeout
print(requests.one('https://httpbin.org/delay/3').content)
# Default timeout implementation
from requests.adapters import HTTPAdapter
class _DefaultTimeoutHTTPAdapter(HTTPAdapter):
def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):
if timeout is None and hasattr(self, 'defaultTimeout'):
timeout = self.defaultTimeout
return super(_DefaultTimeoutHTTPAdapter, self).send(request, stream = stream, timeout = timeout, verify = verify, cert = cert, proxies = proxies)
adapter = _DefaultTimeoutHTTPAdapter()
requests.session.mount('http://', adapter)
requests.session.mount('https://', adapter)
# Set the timeout to one second
adapter.defaultTimeout = 1.0
# Timeout error
print(requests.one('https://httpbin.org/delay/3').content) This isn't really "simple", but it is the recommended approach to setting a default timeout. And it works in the current version of |
Added Available as of 1.1.0. |
perfect, thanks a lot! |
Try testing simple-requests with 'http://www.google.com:81' as the URL for each fetcher in the swarm - they'll all take forever (literally) and never finish..
requests has nice timeout option for this - would be nice if simple-requests had a reasonable default set which could be overwritten in the Requests class.
Same goes for
verify
which is a requests arg to disable https certificate verification.The text was updated successfully, but these errors were encountered: