【问题标题】:Is it possible to increase the response timeout in Google App Engine?是否可以增加 Google App Engine 中的响应超时?
【发布时间】:2011-02-26 13:27:25
【问题描述】:

在我的本地机器上,脚本运行良好,但在云中它一直是 500。这是一个 cron 任务,所以我不介意它是否需要 5 分钟......

:

知道是否可以增加超时时间吗?

谢谢, 瑞

【问题讨论】:

    标签: python google-app-engine exception timeout cron-task


    【解决方案1】:

    您不能超过 30 秒,但您可以通过使用任务队列来间接增加超时 - 并编写逐步迭代数据集并对其进行处理的任务。每个这样的任务运行当然应该符合超时限制。

    编辑

    更具体地说,您可以使用数据存储区查询游标在同一位置恢复处理:

    http://code.google.com/intl/pl/appengine/docs/python/datastore/queriesandindexes.html#Query_Cursors

    在 SDK 1.3.1 中首次引入:

    http://googleappengine.blogspot.com/2010/02/app-engine-sdk-131-including-major.html

    【讨论】:

      【解决方案2】:

      数据库查询超时的确切规则很复杂,但似乎一个查询不能超过大约 2 分钟,而批处理不能超过大约 30 秒。下面是一些将作业分解为多个查询的代码,使用游标来避免这些超时。

      def make_query(start_cursor):
        query = Foo()
      
        if start_cursor:
          query.with_cursor(start_cursor)
      
        return query
      
      batch_size = 1000
      start_cursor = None
      
      while True:
        query = make_query(start_cursor)
        results_fetched = 0
      
        for resource in query.run(limit = batch_size):
          results_fetched += 1
      
          # Do something
      
          if results_fetched == batch_size:
            start_cursor = query.cursor()
            break
        else:
          break
      

      【讨论】:

      • 说“查询不能超过 30 秒”并不完全准确 - 请参阅此处的讨论,尤其是前面的评论 #8:code.google.com/p/googleappengine/issues/detail?id=12243
      • @tom:那么,一个批处理可以运行 30 秒,而一个查询可以运行大约 4 分钟?如果是这样,你建议我如何编辑我的答案?
      • 假设 @Patrick Costello 是正确的(他确实在 Google 工作:),我建议您进行 2 处更改:1)“数据库查询超时的确切规则很复杂,但大致如下:一个查询不能超过 2.5 分钟,一个批次不能超过 30 秒”。这是一些使用游标将作业分解为多个查询的代码,以避免这些超时。更改 #2) for resource in query.run(limit = batch_size):
      • @tom,你能解释一下为什么我需要在查询中添加.run(limit = batch_size)吗?
      • 好问题! @Patrick Costello 将它包含在他建议的代码中,所以我假设它是正确的,但你检查一下是对的!至于为什么:我的假设是将查询限制为“我们的批次”的大小会阻止 AE 预取超出我们限制的结果/批次。我的理解是 GAE 在处理当前批次时会自动获取下一批,但我不知道所有细节。 GAE 无法预测我们的break,因此我认为 GAE 无法在没有limit 提供的提示的情况下优化其 DB RPC 是有道理的。也许没有猜测的人可以参与进来?
      【解决方案3】:

      下面是我用来解决这个问题的代码,将一个大查询分解为多个小查询。我使用google.appengine.ext.ndb 库——我不知道下面的代码是否需要这样做。

      (如果你不使用ndb,可以考虑切换到它。它是db库的改进版本,迁移到它很容易。更多信息,请参阅https://developers.google.com/appengine/docs/python/ndb。)

      from google.appengine.datastore.datastore_query import Cursor
      
      def ProcessAll():
        curs = Cursor()
        while True:
          records, curs, more = MyEntity.query().fetch_page(5000, start_cursor=curs)
          for record in records:
            # Run your custom business logic on record.
            RunMyBusinessLogic(record)
          if more and curs:
            # There are more records; do nothing here so we enter the 
            # loop again above and run the query one more time.
            pass
          else:
            # No more records to fetch; break out of the loop and finish.
            break
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-26
        • 2011-05-09
        • 2014-09-09
        • 2010-10-23
        • 1970-01-01
        • 1970-01-01
        • 2013-03-25
        • 2011-03-09
        相关资源
        最近更新 更多