我需要在有限的时间内获得近 100 页并将结果代码作为响应发送回来。Google Apps 一次有 10 个异步请求的限制。我正在考虑队列,但它们在后台工作,也许计费应用程序可以提供帮助?这是我的代码,当有超过 14 个 urls[] 时,它会失败:
_get_fetch_result 中的文件“/base/python_runtime/python_lib/versions/1/google/appengine/api/urlfetch.py”,第 371 行引发 DeadlineExceededError(str(err)) DeadlineExceededError: ApplicationError: 5
class MainPage(webapp.RequestHandler):
results = []
urls = [ "http://google.com/",
"http://yahoo.com",
"http://goo.gl",
"http://stackoverflow.com",
"http://windows.com",
"http://wikipedia.org"
]
counter = len(urls)
def handle_result(self, rpc, rowIndex):
self.counter -= 1
result = rpc.get_result()
if result:
self.results.append(str(rowIndex)+": "+str(result.status_code)+"<br>")
if not self.counter:
self.response.out.write("".join(self.results))
def create_callback(self, rpc, rowIndex):
return lambda: self.handle_result(rpc, rowIndex)
def get(self):
rpcs = []
rowIndex = 0
for url in self.urls:
rpc = urlfetch.create_rpc(deadline = 10)
rpc.callback = self.create_callback(rpc, rowIndex)
urlfetch.make_fetch_call(rpc, url)
rpcs.append(rpc)
rowIndex += 1
# Finish all RPCs, and let callbacks process the results.
for rpc in rpcs:
rpc.wait()