Google app engine AppEngine';s urlfetch,如何强制遵守超时

Google app engine AppEngine';s urlfetch,如何强制遵守超时,google-app-engine,urlfetch,Google App Engine,Urlfetch,我使用的是Google Appengine的Urlfetch,我添加了deadline参数以强制将截止日期缩短(3秒),如下所示: try: urlfetch.set_default_fetch_deadline(3) return urlfetch.fetch(url='http://www.example.com', method=urlfetch.GET, deadline=3) except google.appengine.runtime.apiproxy_errors

我使用的是Google Appengine的Urlfetch,我添加了deadline参数以强制将截止日期缩短(3秒),如下所示:

try:
    urlfetch.set_default_fetch_deadline(3)
    return urlfetch.fetch(url='http://www.example.com', method=urlfetch.GET, deadline=3)
except google.appengine.runtime.apiproxy_errors.DeadlineExceededError:
    pass
except google.appengine.api.urlfetch_errors.DeadlineExceededError:
    pass

return None
但不管怎样,我的线程持续60秒(Appengine上http请求的最大执行时间),然后在死线异常(“请求后运行的线程”)上惨遭失败


有没有办法确保在3秒钟内严格停止上面的查询?

我可以分享我目前在一个生产项目中运行的一些代码。在本例中,我使用了0.5秒的截止时间。上次我检查时,它仍在工作:

t_rpc = urlfetch.create_rpc(deadline=0.5)
urlfetch.make_fetch_call(t_rpc, url)
# and then later...
try:
    result = t_rpc.get_result()
except:
    # handle errors...
    pass

# use the result...

这实际上是在使用异步版本的API,因此您可以在调用
make\u fetch\u call
get\u result
之间进行技术操作。否则,您可以直接背对背调用它们,它的功能与同步API相同。

您确定代码不会实际引发异常,而是捕获并忽略异常吗?也许可以尝试用一些要检查的
日志调用替换
pass
语句?我相信
set\u default\u fetch\u deadline()
适用于使用
rpc
urlfetch.make\u fetch\u call()
时。您的
urlfetch.fetch(…deadline=3)
应该足够了。尝试添加一个通用的
例外:
并在其中登录,以及其他
例外
否显然截止日期参数已被完全删除,现在需要使用
设置\u默认值\u获取\u截止日期
。请参阅文档:我已经用logging.exception({the name of the exception})替换了“pass”,并且得到了
google.appengine.api.urlfetch\u errors.deadlineexceederror
one。但奇怪的是,代码显然卡在这里,就像logging.exception在600秒后(在队列中)引发异常一样。根本不是这样!我进步了一点,但仍然坚持这一点。我发现异常首先在3秒(我的截止日期)后引发,然后代码挂起在我添加的logging.info上,直到达到最大限制(在我的情况下为600秒)。为什么?我不知道!谢谢你的代码,我试试看。我计划使用异步方法调用URL,我不介意得到结果。如果我从不调用
rpc.get_result()
,你认为这样可以吗?或者这会导致一些内存泄漏?你的代码工作得很好,我的原始代码也很好。问题出在Sentry上,这是一个跟踪错误的服务,是它造成了问题!