简体   繁体   English

如何增加imaplib请求的超时时间?

[英]How do I increase the timeout for imaplib requests?

I'm using imaplib to query Gmail's IMAP, but some requests are taking more than 60 seconds to return. 我正在使用imaplib查询Gmail的IMAP,但某些请求要花费60秒钟以上才能返回。 This is already done in a task, so I have a full 10 minutes to do the request, but my tasks are failing due to the 60 second limit on urlfetch . 这已经在任务中完成,因此我有整整10分钟的时间来完成请求,但是由于urlfetch的60秒限制,我的任务失败了。

I've tried setting urlfetch.set_default_fetch_deadline(600) , but it doesn't seem to do anything. 我尝试设置urlfetch.set_default_fetch_deadline(600) ,但它似乎没有任何作用。

Here's a stacktrace: 这是一个堆栈跟踪:

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.

Which kind of DeadlineExceededError? 哪种DeadlineExceededError?

There are three kinds of DeadlineExceededError in AppEngine. AppEngine中有3种DeadlineExceededError
https://developers.google.com/appengine/articles/deadlineexceedederrors 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. google.appengine.runtime.DeadlineExceededError:如果总体请求超时,则通常在60秒或10分钟后(对于任务队列请求)超时而引发。
  2. google.appengine.runtime.apiproxy_errors.DeadlineExceededError: raised if an RPC exceeded its deadline. google.appengine.runtime.apiproxy_errors.DeadlineExceededError:如果RPC超过其截止日期,则引发。 This is typically 5 seconds, but it is settable for some APIs using the 'deadline' option. 这通常是5秒钟,但是对于某些API,可以使用“最后期限”选项来设置。
  3. google.appengine.api.urlfetch_errors.DeadlineExceededError: raised if the URLFetch times out. google.appengine.api.urlfetch_errors.DeadlineExceededError:如果URLFetch超时则引发。

As you can see. 如你看到的。 The 10mins limit of taskqueue only help the google.appengine.runtime.DeadlineExceededError . 任务taskqueue的10分钟限制仅有助于google.appengine.runtime.DeadlineExceededError The type of DeadlineExceededError can be identified via traceback (list below). DeadlineExceededError的类型可以通过追溯(下面列出)来识别。 In this case, it is google.appengine.runtime.apiproxy_errors.DeadlineExceededError . 在这种情况下,它是google.appengine.runtime.apiproxy_errors.DeadlineExceededError Which will raise in 5secs by default. 默认情况下会提高5秒。 (I will update the post after figure out how to change it) (我会在弄清楚如何更改后更新帖子)

TYPE 1. google.appengine.runtime.DeadlineExceededError 类型1. google.appengine.runtime.DeadlineExceededError

The traceback looks like 追溯看起来像

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

Solution

This exception can be solved by using taskqueue (10mins), backend or manual scaling options. 可以通过使用任务队列(10分钟),后端或手动缩放选项来解决此异常。 https://developers.google.com/appengine/docs/python/modules/#Python_Instance_scaling_and_class https://developers.google.com/appengine/docs/python/modules/#Python_Instance_scaling_and_class

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

The traceback looks like 追溯看起来像

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

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

The traceback looks like 追溯看起来像

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

Solution: 解:

This exception can be solved by extend the deadline. 可以通过延长期限来解决此异常。

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

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

There's no mentioning of timeout in imaplib sources. 在imaplib源文件中没有提及超时。 So there are several options: 因此,有几种选择:

  1. IMAP uses socket library to establish the connection, consider using socket.setdefaulttimeout(timeoutValue) but if you do so be aware of side-effects. IMAP使用套接字库建立连接,请考虑使用socket.setdefaulttimeout(timeoutValue),但如果这样做,请注意副作用。
  2. The second option is to create your own IMAP4 class child with tunable timeout, shall we say in open function 第二种选择是创建自己的具有可调超时的IMAP4类子级,我们应该在开放函数中

From the Google App Engine documentation , it seems like there are many possible causes for DeadlineExceededError . Google App Engine文档看来, DeadlineExceededError可能有很多原因。

In your case, it seems that it may be one of the last two (out of three) types of DeadlineExceededError presented on the page. 在您的情况下,它可能是页面上显示的DeadlineExceededError的最后两种(三种)之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM