我正在使用grequests模块来发出异步请求。测试下面的代码时,会根据TIMEOUT值显示ANOMALY:
>>> grequests.map((grequests.get('http://httpbin.org/delay/1',timeout=0.6),),exception_handler=exception_handler)
failed: http://httpbin.org/delay/1
[<Response [200]>]
>>> grequests.map((grequests.get('http://httpbin.org/delay/1', timeout=0.001),),exception_handler=exception_handler)
failed: http://httpbin.org/delay/1
[None]
那么超时值如何影响exception_handling最后一部分的执行?
>>> def exception_handler(r,e):
print('failed: ',r.url,'\n')
#changing the url just for doing sth
r.url = 'http://httpbin.org/status/200'
res = r.send().response
return res
答案 0 :(得分:0)
我想原因是您只更改了url
的{{1}},但您没有更改它的r
。 timeout
的值存储在timeout
。
根据defination:
self.kwargs
""" Asynchronous request.
Accept same parameters as ``Session.request`` and some additional:
:param session: Session which will do request
:param callback: Callback called on response.
Same as passing ``hooks={'response': callback}``
"""
def __init__(self, method, url, **kwargs):
#: Request method
self.method = method
#: URL to request
self.url = url
#: Associated ``Session``
self.session = kwargs.pop('session', None)
if self.session is None:
self.session = Session()
callback = kwargs.pop('callback', None)
if callback:
kwargs['hooks'] = {'response': callback}
#: The rest arguments for ``Session.request``
self.kwargs = kwargs
#: Resulting ``Response``
self.response = None
的值存储在timeout
中。当您更改self.kwargs
中存储在url
中的self.url
值时,它不会更改。