代码之家  ›  专栏  ›  技术社区  ›  rui

谷歌应用引擎是否可以增加响应超时?

  •  4
  • rui  · 技术社区  · 14 年前

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

    <类“google.appengine.runtime.deadlineexceedderror”>:

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

    谢谢, 芮

    3 回复  |  直到 9 年前
        1
  •  8
  •   Tomasz Zieliński    14 年前

    您不能超过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
  •  7
  •   phatmann Trevor    9 年前

    数据库查询超时的确切规则很复杂,但似乎一个查询不能超过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
    
        3
  •  1
  •   Martin Omander    10 年前

    下面是我用来解决这个问题的代码,将一个大型查询拆分为多个小型查询。我用 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