Exception会捕获DeadlineExceededError异常吗?

时间:2014-05-20 15:59:30

标签: google-app-engine exception exception-handling urlfetch

我有以下代码:

try:
    response = urlfetch.Fetch(url, deadline=60, headers=headers, follow_redirects=False)
except Exception, error_message:
    logging.exception('Failed, exception happened - %s' % error_message)

但有时会因DeadlineExceededError而失败?我的代码不应该抓住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.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~myapp/1.375970700643773844/myapp.py", line 424, in get
    url, response = call_api(origin=origin, destination=destination, date=date_.strftime('%Y-%m-%d'))
  File "/base/data/home/apps/s~myapp/1.375970700643773844/myapp.py", line 288, in call_api
    response = urlfetch.Fetch(url, deadline=60, headers=headers, follow_redirects=False)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/urlfetch.py", line 270, in fetch
    return rpc.get_result()
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 612, in get_result
    return self.__get_result_hook(self)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/urlfetch.py", line 375, in _get_fetch_result
    rpc.check_success()
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 585, in check_success
    self.__rpc, err)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 196, in Call
    for key, function, srv, num_args in self.__content:
DeadlineExceededError

2 个答案:

答案 0 :(得分:3)

要回答您的具体问题,“我的代码不应该捕获此DeadlineExceededError吗?”:

DeadlineExceededError类扩展BaseException而不是Exception,因此永远不会调用您的except子句。

from google.appengine.runtime import DeadlineExceededError
my_error = DeadlineExceededError()
isinstance(my_error, Exception)  # False
isinstance(my_error, BaseException)  # True

答案 1 :(得分:2)

第一种方法

尝试通过导入

排除DeadlineExceededError

from google.appengine.runtime import DeadlineExceededError这样做:

try:
    response = urlfetch.Fetch(url, deadline=60, headers=headers, follow_redirects=False)
except DeadlineExceededError, error_message:
    logging.exception('Failed, exception happened - %s' % error_message)

documentation上了解详情。

第二种方法

之前我遇到过这个错误,我接下来的方法是不设置我试图捕获的异常类。我刚刚打电话给except

try:
    response = urlfetch.Fetch(url, deadline=60, headers=headers, follow_redirects=False)
except:
    logging.exception('First failure')
    # try again your request as suggested by the documentation
    try:
        response = urlfetch.Fetch(url, deadline=60, headers=headers, follow_redirects=False)
    except:
        logging.exception('Second failure')
        return None

但是我无法捕获错误消息,而不是仅仅记录错误消息,我在引发异常时再次尝试了请求,正如我上面发布的链接所示。

您应阅读的一些好的链接

您应该阅读Dealing with DeadlineExceedError

还有Backends,允许您避免此请求计时器;对于后端,生成和返回请求没有时间限制。

希望这会对你有所帮助。