【问题标题】:Unable to handle DeadlineExceededError while using UrlFetch使用 UrlFetch 时无法处理 DeadlineExceededError
【发布时间】:2011-04-21 00:23:47
【问题描述】:

我有这个基本的实用程序类并行获取(可能)缩短的 URL 并返回一个包含最终 URL 的字典。它使用 blog post 中描述的 wait_any 功能。

class UrlFetcher(object):

  @classmethod
  def fetch_urls(cls,url_list):
    rpcs = []
    for url in url_list:
      rpc = urlfetch.create_rpc(deadline=5.0)
      urlfetch.make_fetch_call(rpc, url,method = urlfetch.HEAD)
      rpcs.append(rpc)

    result = {}
    while len(rpcs) > 0:
      rpc = apiproxy_stub_map.UserRPC.wait_any(rpcs)
      rpcs.remove(rpc)
      request_url = rpc.request.url()
      try:
        final_url = rpc.get_result().final_url
      except AttributeError:
        final_url = request_url
      except DeadlineExceededError:
        logging.error('Handling DeadlineExceededError for url: %s' %request_url)
        final_url  = None
      except (DownloadError,InvalidURLError):
        final_url  = None        
      except UnicodeDecodeError: #Funky url with very evil characters
        final_url = unicode(rpc.get_result().final_url,'utf-8')

      result[request_url] = final_url

    logging.info('Returning results: %s' %result)
    return result

即使我尝试处理 DeadlineExceededError,应用程序日志也会显示。

2011-04-20 17:06:17.755
UrlFetchWorker started
E 2011-04-20 17:06:22.769
The API call urlfetch.Fetch() took too long to respond and was cancelled.
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/tweethitapp/1.349863151373877476/tweethit/handlers/taskworker.py", line 80, in post
    result_dict = UrlFetcher.fetch_urls(fetch_targets)
  File "/base/data/home/apps/tweethitapp/1.349863151373877476/tweethit/utils/rpc.py", line 98, in fetch_urls
    final_url = rpc.get_result().final_url
  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 345, in _get_fetch_result
    rpc.check_success()
  File "/base/python_runtime/python_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 558, in check_success
    self.__rpc.CheckSuccess()
  File "/base/python_runtime/python_lib/versions/1/google/appengine/api/apiproxy_rpc.py", line 133, in CheckSuccess
    raise self.exception
DeadlineExceededError: The API call urlfetch.Fetch() took too long to respond and was cancelled.
W 2011-04-20 17:06:22.858
Found 1 RPC request(s) without matching response (presumably due to timeouts or other errors)

我在这里缺少什么?有没有其他方法来处理 DeadlineExceededError?

或者是否有不同类型的 DeadlineExceededErrors 并且我导入了错误的错误?我正在使用:from google.appengine.runtime import DeadlineExceededError

【问题讨论】:

    标签: python google-app-engine


    【解决方案1】:

    根据google.appengine.runtime.DeadlineExceededError 的内联文档:

    请求时引发异常 达到其总时间限制。

    不要混淆 runtime.apiproxy_errors.DeadlineExceededError。 那个是在单独的 API 时提出的 通话时间过长。

    这很好地说明了为什么应该使用合格的导入(from google.appengine import runtime,然后引用 runtime.DeadlineExceededError)!

    【讨论】:

    • Importing "from google.appengine.runtime import apiproxy_errors" 然后使用 "except apiproxy_errors.DeadlineExceededError:" 子句似乎捕获了错误。谢谢!
    【解决方案2】:

    正如你猜想和其他人所说,你想要一个不同的DeadlineExceededError

    来自https://developers.google.com/appengine/articles/deadlineexceedederrors,日期为 2012 年 6 月:

    目前,Python 运行时有几个名为 DeadlineExceededError 的错误:>

    google.appengine.runtime.DeadlineExceededError:如果整个请求超时(通常在 60 秒后或任务队列请求为 10 分钟后)引发;

    google.appengine.runtime.apiproxy_errors.DeadlineExceededError:如果 RPC 超过其截止日期,则引发。这通常为 5 秒,但对于某些使用“截止日期”选项的 API 是可设置的;

    google.appengine.api.urlfetch_errors.DeadlineExceededError:如果 URLFetch 超时则引发。

    捕捉google.appengine.api.urlfetch_errors.DeadlineExceededError 似乎对我有用。另外值得注意的是(至少在开发应用服务器 1.7.1 中),urlfetch_errors.DeadlineExceededErrorDownloadError 的子类,这是有道理的:

    class DeadlineExceededError(DownloadError):
      """Raised when we could not fetch the URL because the deadline was exceeded.
    
      This can occur with either the client-supplied 'deadline' or the system
      default, if the client does not supply a 'deadline' parameter.
      """
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多