在使用UrlFetch时无法处理DeadlineExceededError [英] Unable to handle DeadlineExceededError while using UrlFetch

查看:87
本文介绍了在使用UrlFetch时无法处理DeadlineExceededError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  class UrlFetcher(object):

@classmethod
对于url_list中的url,$ f $ b rpcs = []

rpc = urlfetch.create_rpc(截止日期= 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()
尝试:
final_url除了AttributeError:
final_url = request_url
,除了DeadlineExceededError:
logging.error('处理DeadlineExceededError for url:%s'%request_url)$:$ rbc.get_result b $ b final_url = None
除了(DownloadError,InvalidURLError):
final_url =无
除了UnicodeDecodeError:#带有非常邪恶字符的流氓URL
final_url = unicode(rpc.get_result( ).final_url,'utf-8')

结果[request_url] = final_url

logging.info('返回结果:%s'%结果)
返回结果

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

  2011-04-20 17:06:17.755 
UrlFetchWorker开始
E 2011-04-20 17:06:22.769
调用urlfetch.Fetch()的时间太长,无法响应并被取消。
Traceback(最近一次调用最后):
文件/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/__init__.py,第636行,在__call__ $ b $中b handler.post(* groups)
文件/base/data/home/apps/tweethitapp/1.349863151373877476/tweethit/handlers/taskworker.py,第80行,后
result_dict = UrlFetcher.fetch_urls (fetch_targets)
文件/base/data/home/apps/tweethitapp/1.349863151373877476/tweethit/utils/rpc.py,第98行,在fetch_urls
final_url = rpc.get_result()。final_url
文件/base/python_runtime/python_lib/versions/1/google/appengine/api/apiproxy_stub_map.py,第592行,在get_result中
返回self .__ get_result_hook(self)
文件/ base / python_runtime / python_lib / versions / 1 / google / appengine / api / urlfetch.py​​,行345,位于_get_fetch_result
rpc.check_success()
文件/ base / python_runtime / python_lib / versions / 1 / google / appengine / api / apiproxy_stub_map.py,第558行,在check_success
self.__rpc.Ch eckSuccess()
CheckSuccess中的文件/base/python_runtime/python_lib/versions/1/google/appengine/api/apiproxy_rpc.py,第133行
raise self.exception
DeadlineExceededError: API调用urlfetch.Fetch()花费很长时间来响应并被取消。
W 2011-04-20 17:06:22.858
发现1个没有匹配响应的RPC请求(可能是由于超时或其他错误)

我在这里错过了什么?有没有其他方式来处理DeadlineExceededError?

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

解决方案

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


例外当请求
达到其整体时间限制时产生。



不要与
runtime.apiproxy_errors.DeadlineExceededError混淆。
当单个API
调用需要很长时间时才会产生。


这是为什么您应该使用合格的导入( import runtime ,然后引用 runtime.DeadlineExceededError ) p>

I have this basic utility class fetches (possibly) shortened URLs in parallel and returns a dictionary that has final URLs. It uses the wait_any functionality that was described in this blog post.

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

Even if I try to handle DeadlineExceededError, the application logs show otherwise.

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)

What am I missing here? Is there any other way to handle DeadlineExceededError?

Or are there different types of DeadlineExceededErrors and I'm importing the wrong one? I'm using: from google.appengine.runtime import DeadlineExceededError

解决方案

Per the inline docs for google.appengine.runtime.DeadlineExceededError:

Exception raised when the request reaches its overall time limit.

Not to be confused with runtime.apiproxy_errors.DeadlineExceededError. That one is raised when individual API calls take too long.

This is a good demonstration of why you should use qualified imports (from google.appengine import runtime, then reference runtime.DeadlineExceededError), too!

这篇关于在使用UrlFetch时无法处理DeadlineExceededError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆