python – 如何增加imaplib请求的超时?

我正在使用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中有三种DeadlineExceededError.
https://developers.google.com/appengine/articles/deadlineexceedederrors

  1. google.appengine.runtime.DeadlineExceededError: raised if the overall request times out, typically after 60 seconds, or 10 minutes
    for task queue requests.
  2. google.appengine.runtime.apiproxy_errors.DeadlineExceededError: raised if an RPC exceeded its deadline. This is typically 5 seconds,
    but it is settable for some APIs using the ‘deadline’ option.
  3. google.appengine.api.urlfetch_errors.DeadlineExceededError: raised if the URLFetch times out.

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

点赞