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

最佳答案

哪种 DeadlineExceededError?

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



如你看到的。 taskqueue 的 10 分钟限制仅对 google.appengine.runtime.DeadlineExceededError 有帮助。 DeadlineExceededError 的类型可以通过回溯识别(列表如下)。在这种情况下,它是 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.

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

10-08 12:43