如何增加imaplib请求的超时?

时间:2013-10-18 09:05:29

标签: python google-app-engine

我正在使用imaplib来查询Gmail的IMAP,但有些请求需要超过60秒才能返回。这已在任务中完成,因此我有10分钟的时间来完成请求,但由于urlfetch的60秒限制,我的任务失败了。

我已尝试设置urlfetch.set_default_fetch_deadline(600),但它似乎没有做任何事情。

这是一个堆栈跟踪:

The API call remote_socket.Receive() took too long to respond and was cancelled.
Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/imaplib.py", line 760, in uid
    typ, dat = self._simple_command(name, command, *args)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/imaplib.py", line 1070, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/imaplib.py", line 897, in _command_complete
    typ, data = self._get_tagged_response(tag)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/imaplib.py", line 999, in _get_tagged_response
    self._get_response()
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/imaplib.py", line 916, in _get_response
    resp = self._get_line()
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/imaplib.py", line 1009, in _get_line
    line = self.readline()
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/imaplib.py", line 1171, in readline
    return self.file.readline()
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/socket.py", line 445, in readline
    data = self._sock.recv(self._rbufsize)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/ssl.py", line 301, in recv
    return self.read(buflen)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/ssl.py", line 220, in read
    return self._sslobj.read(len)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/remote_socket/_remote_socket.py", line 864, in recv
    return self.recvfrom(buffersize, flags)[0]
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/remote_socket/_remote_socket.py", line 903, in recvfrom
    apiproxy_stub_map.MakeSyncCall('remote_socket', 'Receive', request, reply)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 94, in MakeSyncCall
    return stubmap.MakeSyncCall(service, call, request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 328, in MakeSyncCall
    rpc.CheckSuccess()
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/apiproxy_rpc.py", line 133, in CheckSuccess
    raise self.exception
DeadlineExceededError: The API call remote_socket.Receive() took too long to respond and was cancelled.

3 个答案:

答案 0 :(得分:2)

哪种DeadlineExceededError?

AppEngine中有三种DeadlineExceededError https://developers.google.com/appengine/articles/deadlineexceedederrors

  
      
  1. google.appengine.runtime.DeadlineExceededError:如果整体请求超时(通常在60秒或10分钟后),则会引发   用于任务队列请求。
  2.   
  3. google.appengine.runtime.apiproxy_errors.DeadlineExceededError:如果RPC超过其截止日期,则会引发此问题。这通常是5秒,   但是可以使用'deadline'选项为某些API设置。
  4.   
  5. google.appengine.api.urlfetch_errors.DeadlineExceededError:如果URLFetch超时则引发。
  6.   

如你所见。 taskqueue的10分钟限制仅对google.appengine.runtime.DeadlineExceededError有帮助。可以通过追溯(下面的列表)识别DeadlineExceededError的类型。在这种情况下,它是google.appengine.runtime.apiproxy_errors.DeadlineExceededError。默认情况下会增加5秒。 (我会在弄清楚如何更改之后更新帖子)

TYPE 1. google.appengine.runtime.DeadlineExceededError

回溯看起来像

Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 266, in Handle
    result = handler(dict(self._environ), self._StartResponse)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
    return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~tagtooadex2/test.371204033771063679/index.py", line 9, in get
    pass
DeadlineExceededError

解决方案

可以使用taskqueue(10分钟),后端或手动缩放选项来解决此异常。 https://developers.google.com/appengine/docs/python/modules/#Python_Instance_scaling_and_class

TYPE 2. google.appengine.runtime.apiproxy_errors.DeadlineExceededError

回溯看起来像

DeadlineExceededError: The API call remote_socket.Receive() took too long to respond and was cancelled.

TYPE 3. google.appengine.api.urlfetch_errors.DeadlineExceededError

回溯看起来像

DeadlineExceededError: Deadline exceeded while waiting for HTTP response from URL:     http://www.sogi.com.tw/newforum/article_list.aspx?topic_ID=6089521

解决方案:

这个例外可以通过延长截止日期来解决。

urlfetch.fetch(url, deadline=10*60)

https://developers.google.com/appengine/docs/python/urlfetch/fetchfunction

答案 1 :(得分:0)

在imaplib来源中没有提到超时。所以有几种选择:

  1. IMAP使用套接字库建立连接,考虑使用 socket.setdefaulttimeout(timeoutValue)但如果你这样做的话 意识到副作用。
  2. 第二个选项是创建自己的IMAP4类子项 可调超时,我们应该在open function
  3. 中说

答案 2 :(得分:0)

Google App Engine documentation开始,似乎有很多 DeadlineExceededError的可能原因。

在您的情况下,它似乎可能是页面上显示的最后两种(三种)DeadlineExceededError类型之一。

相关问题