Python 如何增加imaplib请求的超时?
我正在使用Python 如何增加imaplib请求的超时?,python,google-app-engine,Python,Google App Engine,我正在使用imaplib查询Gmail的IMAP,但有些请求需要60秒以上才能返回。这已在任务中完成,因此我有整整10分钟的时间来执行请求,但由于urlfetch上的60秒限制,我的任务失败 我试过设置urlfetch.set\u default\u fetch\u deadline(600),但它似乎没有任何作用 这里有一个stacktrace: The API call remote_socket.Receive() took too long to respond and was canc
imaplib
查询Gmail的IMAP,但有些请求需要60秒以上才能返回。这已在任务中完成,因此我有整整10分钟的时间来执行请求,但由于urlfetch
上的60秒限制,我的任务失败
我试过设置urlfetch.set\u default\u fetch\u deadline(600)
,但它似乎没有任何作用
这里有一个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.
imaplib源代码中没有提到超时。因此,有几种选择:
死线超出DederRor
的可能原因
在您的情况下,它可能是页面上显示的最后两种(三种类型中的一种)类型的deadlineexceedderror
。哪种类型的deadlineexceedderror?
AppEngine中有三种类型的死线超出derror
。taskqueue
的10分钟限制只对google.appengine.runtime.DeadlineExceededError
有帮助。死线超标错误的类型可以通过回溯(如下列表)进行识别。在本例中,它是google.appengine.runtime.apiproxy\u errors.DeadlineExceededError
。默认情况下,它将在5秒内升高。(我会在弄清楚如何更改后更新帖子)
类型1。google.appengine.runtime.deadlineExceederRor
回溯看起来像
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
DeadlineExceededError: Deadline exceeded while waiting for HTTP response from URL: http://www.sogi.com.tw/newforum/article_list.aspx?topic_ID=6089521
解决方案
使用taskqueue(10分钟)、后端或手动缩放选项可以解决此异常。
类型2。google.appengine.runtime.apiproxy_errors.deadlineExceederRor
回溯看起来像
DeadlineExceededError: The API call remote_socket.Receive() took too long to respond and was cancelled.
类型3。google.appengine.api.urlfetch_errors.deadlineExceederRor
回溯看起来像
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
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)
我不认为是套接字库超时了。(It)。似乎是阿彭尼把死线抛得太远了。所有请求限制为60秒。但由于我是在taskqueue中执行此操作,我应该有最多10分钟的时间来执行请求。我认为,您更希望添加一些代码来重现问题,这将增加您获得答案的机会。我尝试了imaplib,但无法重现错误。它可以一直执行到请求超时。类型2。问题是,没有(记录在案的)方法来设置套接字的超时。事实上,据文件记载,默认情况下套接字没有超时。所以我不明白为什么我的一些请求会在60秒内超时。一个谷歌的家伙说事实上imap端口被阻塞了。。。我试图访问gmail帐户,但没有收到IMAP4_SSL错误。如果您的imap服务器是gmail,请尝试: