回复的问题'没有'甚至在使用exception_handler之后输入

时间:2017-07-13 06:48:18

标签: python grequests

我正在使用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

1 个答案:

答案 0 :(得分:0)

我想原因是您只更改了url的{​​{1}},但您没有更改它的rtimeout的值存储在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值时,它不会更改。