Timeout when using urllib2.urlopen with Django in GAE
When I run this code
url = ('http://maps.google.com/maps/nav?'+
'q=from%3A'+from_address+
'+to%3A开发者_StackOverflow'+to_address+
'&output=json&oe=utf8&key='+api_key)
request = urllib2.Request(url)
response = urllib2.urlopen(request)
In a simple view in Django running in google app engine via the Google App Engine Helper for Django I get an ApplicationError: 2 timed out
exception, but when I run the same code in python's or Django's shell it works just fine.
Any ideas what's going on? Thanks!
This is because App Engine has a default timeout of 5 seconds for these calls. If you use UrlFetch [1] you can use the deadline parameter to set the timeout up to a maximum of 10 seconds. If the page you are trying to get takes longer than that, you are out of luck.
[1] http://code.google.com/appengine/docs/python/urlfetch/fetchfunction.html
you can avoid the 10 second deadline by using googles asynchronous fetch service, which accepts deadlines of up to 60 seconds. https://developers.google.com/appengine/docs/python/urlfetch/asynchronousrequests
精彩评论