在GAE中使用urllib2.urlopen与Django时超时

时间:2010-01-29 16:39:26

标签: python django google-app-engine urllib2

当我运行此代码时

url = ('http://maps.google.com/maps/nav?'+
       'q=from%3A'+from_address+
       '+to%3A'+to_address+
       '&output=json&oe=utf8&key='+api_key)
request = urllib2.Request(url)
response = urllib2.urlopen(request)

在Django的简单视图中,通过 Google App Engine Helper for Django 在谷歌应用引擎中运行我得到ApplicationError: 2 timed out异常,但是当我在python或Django中运行相同的代码时它工作得很好。

任何想法发生了什么?谢谢!

2 个答案:

答案 0 :(得分:2)

这是因为这些呼叫的App Engine默认超时为5秒。如果您使用UrlFetch [1],您可以使用deadline参数将超时设置为最多10秒。如果您尝试获取的页面花费的时间比此长,那么您就不幸了。

[1] http://code.google.com/appengine/docs/python/urlfetch/fetchfunction.html

答案 1 :(得分:1)

您可以通过使用谷歌异步提取服务来避免10秒的截止日期,该服务接受最多60秒的截止日期。 https://developers.google.com/appengine/docs/python/urlfetch/asynchronousrequests