4

imaplib用来查询 Gmail 的 IMAP,但有些请求需要 60 多秒才能返回。这已经在一项任务中完成,所以我有整整 10 分钟的时间来完成请求,但是由于urlfetch.

我试过设置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.
4

3 回答 3

2

哪种 DeadlineExceededError?

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

  1. google.appengine.runtime.DeadlineExceededError:如果整个请求超时,通常在 60 秒后引发,对于任务队列请求则为 10 分钟。
  2. google.appengine.runtime.apiproxy_errors.DeadlineExceededError:如果 RPC 超过其截止日期,则会引发此错误。这通常为 5 秒,但对于某些使用“截止日期”选项的 API 是可设置的。
  3. google.appengine.api.urlfetch_errors.DeadlineExceededError:如果 URLFetch 超时则引发。

如你看到的。10分钟的限制taskqueue只能帮助google.appengine.runtime.DeadlineExceededErrorDeadlineExceededError可以通过回溯(下面的列表)识别类型。在这种情况下,它是google.appengine.runtime.apiproxy_errors.DeadlineExceededError。默认情况下会在 5 秒内提升。(我会在弄清楚如何更改后更新帖子)

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

解决方案

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

类型 2.google.appengine.runtime.apiproxy_errors.DeadlineExceededError

回溯看起来像

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

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

于 2013-10-27T04:04:27.647 回答
0

Google App Engine 文档来看,似乎有很多可能的原因DeadlineExceededError

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

于 2013-10-26T17:30:29.423 回答
0

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

  1. IMAP 使用套接字库来建立连接,请考虑使用 socket.setdefaulttimeout(timeoutValue)但如果您这样做,请注意副作用。
  2. 第二个选项是创建您自己的具有可调超时的 IMAP4 类子,我们应该在open 函数中说
于 2013-10-21T21:42:36.937 回答