0

我需要在有限的时间内获得近 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()
4

1 回答 1

0

您可以将任务排入队列,然后使用通道 API通知并将结果发送给用户。目前,频道仅适用于 Javascript 客户端。Google 计划在其他语言上实现频道客户端,或者至少为任何想要编写它们的实现的人记录频道客户端。

于 2012-02-29T17:22:48.023 回答