Skip to main content
 首页 » 编程设计

python之在 GAE 中将 urllib2.urlopen 与 Django 一起使用时超时

2024年09月03日28sky-heaven

当我运行这段代码时

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) 

在通过 Google App Engine Helper for Django 在 Google App Engine 中运行的 Django 中的一个简单 View 中,我得到一个 ApplicationError: 2 timed out 异常,但是当我运行在 python 或 Django 的 shell 中使用相同的代码它工作得很好。

有什么想法吗?谢谢!

请您参考如下方法:

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

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