Google Appengine URLFetch 超时 - 有什么最佳实践?
我刚开始学习Python和App Engine,最近在玩一个小项目,昨晚遇到了一些脚本超时的问题。我知道你最多只能运行10秒钟。处理这个问题的最佳方法是什么呢?
补充说明
抱歉,我应该说得更清楚一点。我遇到的问题是URLFetch超时。默认情况下,它设置为5秒,最大为10秒。
Traceback (most recent call last):
File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 636, in __call__
handler.post(*groups)
File "/base/data/home/apps/netlicense/3.349495357411133950/main.py", line 235, in post
graph.put_wall_post(message=body, attachment=attch, profile_id=self.request.get("fbid"))
File "/base/data/home/apps/netlicense/3.349495357411133950/facebook.py", line 149, in put_wall_post
return self.put_object(profile_id, "feed", message=message, **attachment)
File "/base/data/home/apps/netlicense/3.349495357411133950/facebook.py", line 131, in put_object
return self.request(parent_object + "/" + connection_name, post_args=data)
File "/base/data/home/apps/netlicense/3.349495357411133950/facebook.py", line 179, in request
file = urllib2.urlopen(urlpath, post_data)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 124, in urlopen
return _opener.open(url, data)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 381, in open
response = self._open(req, data)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 399, in _open
'_open', req)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 360, in _call_chain
result = func(*args)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 1115, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 1080, in do_open
r = h.getresponse()
File "/base/python_runtime/python_dist/lib/python2.5/httplib.py", line 197, in getresponse
self._allow_truncated, self._follow_redirects)
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/urlfetch.py", line 260, in fetch
return rpc.get_result()
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 592, in get_result
return self.__get_result_hook(self)
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/urlfetch.py", line 361, in _get_fetch_result
raise DeadlineExceededError(str(err))
DeadlineExceededError: ApplicationError: 5
1 个回答
4
你没有告诉我们你的应用程序具体做什么,所以这里有一些通用的建议:
- 你可以用这个异常类
google.appengine.api.urlfetch.DownloadError
来捕捉超时的错误,并友好地提醒用户重试。 - 网页请求的最大运行时间是30秒;如果你要下载的内容比较小,你可以捕捉这个错误,然后在同一个网页请求中重新提交一次urlfetch。
- 如果你的应用程序不需要在线工作,你可以把Urlfetch的调用放到一个由 任务队列 处理的工作任务中;使用任务队列的一个好处是,App Engine会自动重试Urlfetch任务,直到成功为止。