use*_*091 2 python google-app-engine urllib2 urlfetch
我在python中使用urllib2用于Google App Engine(GAE).由于以下错误,应用程序经常崩溃:
等待来自URL的HTTP响应时超过截止时间:....
Source看起来像这样:
import webapp2
import urllib2
from bs4 import BeautifulSoup
def functionRunning2To5Seconds_1()
#Check if the Url could be parsed
try:
url ="http://...someUrl..."
req = urllib2.Request(url,headers={'User-Agent': 'Mozilla/5.0'})
page = urllib2.urlopen(req)
htmlSource = BeautifulSoup(page)
except Exception e:
logging.info("Error : {er}".format(er=str(e)))
#do some calculation with the data of htmlSource, which takes 2 To 5 Seconds
#and the handler looks like:
class xyHandler(webapp2.RequestHandler):
def post(self, uurl=None):
r_data1 = functionRunning2To5Seconds_1()
r_data2 = functionRunning2To5Seconds_2()
r_data3 = functionRunning2To5Seconds_3()
...
#show the results in a web page
Run Code Online (Sandbox Code Playgroud)
我发现这个文件说明:
您可以使用Python标准库urllib,urllib2或httplib来发出HTTP请求.在App Engine中运行时,这些库使用App Engine的URL提取服务执行HTTP请求
还有这个:
您可以为请求设置截止日期,即服务等待响应的最长时间.默认情况下,获取的截止时间为5秒.HTTP请求的最长期限为60秒,任务队列和cron作业请求的最长期限为60秒.
那我该怎么做呢?如何在urllib2上设置超时?
或者,我是否必须重写整个应用程序才能使用App Engine的URL提取服务?
(PS:有没有人知道一种安全的方式来并行运行"r_data1 = functionRunning2To5Seconds _...()"调用?)
https://docs.python.org/2/library/urllib2.html
urllib2.urlopen(url[, data][, timeout])
Run Code Online (Sandbox Code Playgroud)
可选的timeout参数指定阻塞操作(如连接尝试)的超时(以秒为单位)(如果未指定,将使用全局默认超时设置).
| 归档时间: |
|
| 查看次数: |
1436 次 |
| 最近记录: |