2017-07-13 3 views
0

私は非同期要求を行うためにgrequestsモジュールを使用しています。次のコードは、TIMEOUT値に基づいて異常を示しているテスト中:例外ハンドラを使用した後でも「なし」タイプを返すgrequest

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

私は理由はあなただけrurlを変更しましたが、あなたはそれのtimeoutを変更していないですね。 timeoutの値はself.kwargsに格納されます。

definationによれば:

""" 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の値をexception_handlerに変更したときは変更されませんでした。

関連する問題