谷歌应用程序引擎任务队列

时间:2012-11-25 15:42:20

标签: python google-app-engine

在谷歌应用引擎中使用任务队列时遇到一些问题。我试着像在这个网站上做同样的事情: https://developers.google.com/appengine/docs/python/taskqueue/overview-push

但似乎我的任务从未执行过,我收到了这个错误:

警告2012-11-25 15:29:21,258 taskqueue_stub.py:1978]任务task1无法执行。此任务将在12.800秒内重试

除了以下代码之外,代码主要是相同的:

class CounterWorker(webapp.RequestHandler):
  def init(self): # should run at most 1/s
    def txn():
      logging.info("bla")
    db.run_in_transaction(txn)

我只是添加这样的任务:

taskqueue.add(url='/worker')

我真正想要的只是运行一段不会超时的代码。所以我读到我可以使用任务队列。但我似乎无法使其发挥作用。

2 个答案:

答案 0 :(得分:5)

默认情况下,任务队列将POST请求发送到映射到URL的任何处理程序(在您的case / worker中,应映射到CounterWorker)。因此,您需要在CounterWorker方法中定义post方法。

class CounterWorker(webapp.RequestHandler):
  def post(self):
    def txn():
      logging.info("bla")
    db.run_in_transaction(txn)

您的评论表明任务应该只运行1 / s。您可以在queue.yaml config file

中对此进行定义

此外,任务队列有10分钟超时,因此它们不会永远运行。要解决此问题,请尝试链接它们或使用deferred API。对于长时间运行的进程,backends API可能更合适。

答案 1 :(得分:4)

如果您只想执行“vanilla”任务,那么请查看延迟函数。

Background work with the deferred library

from google.appengine.ext import deferred

  def do_something_expensive(a, b, c=None):
      logging.info("Doing something expensive!")
      # Do your work here

  # Somewhere else
  deferred.defer(do_something_expensive, "Hello, world!", 42, c=True)

您不需要url / webapp处理程序,因为您可以直接传递该函数。