GAE - AppEngine - DeadlineExceededError: Deadline exceeded while waiting for HTTP response from URL:

南笙酒味 提交于 2019-11-29 01:23:14

Try setting the deadline for urlfetch to 30seconds or more(depending on whether you're calling urlfetch from within a task handler or request handler)

More info about urlfetch:Url Fetch Docs

Simple answer: the default deadline for url fetching is set to 5 seconds.

How to fix:

from google.appengine.api import urlfetch

urlfetch.set_default_fetch_deadline(60)

I am not familiar with the facebook API's. But the way the urlfetch is constructed looks a bit strange. Normaly the method (Post) is a urlfetch argument and the post payload is urlencoded. This results in:

params = urllib.urlencode([
    ('access_token', facebook_access_token),
    ('url', url),
    ('name', caption),
  ])
 response = urlfetch.fetch(url='https://graph.facebook.com/me/photos', payload=params, method=urlfetch.POST,
                      headers={'Content-Type': 'application/x-www-form-urlencoded'})

Possibly related: I was experiencing the same deadline exceeded when testing local urlfetch code. It turns out that while a direct GET of a resource on the dev server via a browser would work, e.g., http://localhost:8080/content/test.jpg, trying the same via urlfetch was doomed to fail every time. I can only assume that fetching from localhost, which is translated to 127.0.0.1 at runtime, is not supported by urlfetch.

It turns out that yours was truly a timeout issue, whereas my answer is to not use urlfetch in dev.

An alternative to setting the deadline globally is to set it on the specific fetch call. In your case:

result = urlfetch.fetch(
    url=your_url,
    deadline=60
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!