Google App Engine deferred.defer() 错误 404

9 投票
3 回答
1385 浏览
提问于 2025-04-17 21:44

我正在尝试使用 deferred.defer() 在任务队列中运行一个任务。这个任务被添加到了默认的任务队列中,但却出现了404错误。

这是处理程序:

import webapp2
import models

import defer_ajust_utils

from google.appengine.ext import ndb
from google.appengine.ext import deferred

class ajust_utils(webapp2.RequestHandler):
  def get(self):
    deferred.defer(defer_ajust_utils.DoTheJob)

application = webapp2.WSGIApplication([('/ajust_utils', ajust_utils)], debug=True)

这是模块 defer_ajust_utils

import logging
import models 
from google.appengine.ext import ndb

def DoTheJob():
  logging.info("Debut de la mise a jour des utilisateurs")
  utilisateurs = models.Utilisateur.query()
  utilisateurs = utilisateurs.fetch()

  for utilisateur in utilisateurs:
    utilisateur.produire_factures_i = False
    utilisateur.put()  

  logging.info("Fin de la mise a jour des utilisateurs")

还有我的 app.yaml 文件:

application: xxxx
version: dev
runtime: python27
api_version: 1
threadsafe: yes

builtins:
- deferred: on

handlers:
- url: /ajust_utils
  script : tempo_ajuster_utils.application
  login: admin

这是日志:

0.1.0.2 - - [10/Mar/2014:17:50:45 -0700] "POST /_ah/queue/deferred HTTP/1.1" 404 113
"http://xxxx.appspot.com/ajust_utils" "AppEngine-Google;
(+http://code.google.com/appengine)" "xxxx.appspot.com" ms=6 cpu_ms=0 
cpm_usd=0.000013 queue_name=default task_name=17914595085560382799 
app_engine_release=1.9.0 instance=00c61b117c0b3648693af0563b92051423b3cb

谢谢你的帮助!!

3 个回答

-1

我觉得你需要在你的应用的 "builtin" 选项里加上一个选项,叫做 deferred: on,这个设置是在 app.yaml 文件中。下面是一个相关的摘录:

https://developers.google.com/appengine/docs/python/config/appconfig#Python_app_yaml_Builtin_handlers

builtins:
- deferred: on

The following builtin handlers are available:
admin_redirect - For production App Engine, this results in a redirect from /_ah/admin to the admin console. This builtin has no effect in the development web server.
appstats - Enables Appstats at /_ah/stats/, which you can use to measure your application's performance. In order to use Appstats, you also need to install the event recorder.
deferred - Enables the deferred handler at /_ah/queue/deferred. This builtin allows developers to use deferred.defer() to simplify the creation of Task Queue tasks. Also see Background work with the deferred library.
3

我也遇到了同样的错误。

看起来这是在https://cloud.google.com/appengine/articles/deferred上的文档问题。

我查看了源代码,发现了一些文档里没有提到的内容:

In order for tasks to be processed, you need to set up the handler. Add the
following to your app.yaml handlers section:

handlers:
- url: /_ah/queue/deferred
  script: $PYTHON_LIB/google/appengine/ext/deferred/handler.py
  login: admin

因为我在我的app.yaml文件中设置了threadsafe: true,所以我需要添加以下处理程序:

- url: /_ah/queue/deferred
  script: google.appengine.ext.deferred.deferred.application
  login: admin

然后,延迟任务队列就开始正常工作,不再出现404错误了。

6

如果你在使用 git 的推送部署功能,当你在 app.yaml 文件中添加一个 'builtin' 部分,比如:

builtins: - deferred: on

你需要先执行一次 '正常的' gcloud 部署,然后再运行应用程序。否则,正在运行的应用程序不会更新,这样就会导致访问 /_ah/queue/deferred 时出现 404 错误。

这个问题已经被报告了,所以可以给它投票,可能会得到修复。你可以在这里查看:https://code.google.com/p/googleappengine/issues/detail?id=10139

撰写回答