0

我正在使用 grequests 模块发出异步请求。在测试以下代码时,显示基于 TIMEOUT 值的异常:

>>> 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]

那么timeout 的值如何影响 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
4

1 回答 1

0

我想原因是你只改变了urlof r,但你没有改变timeout它。的值timeout存储在 中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.kwargsurl当您更改存储在self.url中的值时,它并没有改变exception_handler

于 2017-07-13T07:06:34.570 回答