使用 deferred.defer 任务发布到数据存储时出现问题
我正在尝试使用 Python 在 Google App Engine 上执行此操作:
def add_to_db(person):
a = PersonDb(key_name = person)
# get some data
data1 = a.name
data2 = a.age
a.put()
for person in people:
deferred.defer(add_to_db, person, _queue="myque")
当我通过 cron 作业运行上述代码时,它不起作用。文件执行时没有错误,任务(“People”列表中有 200 个字符串)被正确添加到队列中并正确地通过。但数据库 PersonDb 没有更新。我知道该函数有效(上面是简化的),因为它有效:
for person in people:
add_to_db(person)
上面两行代码有效,并且数据库得到更新,但我需要将其作为延迟任务运行。有什么建议吗?
更新:我在日志中收到以下内容: 文件“C:\Program Files\Google\google_appengine\google\appengine\ext\deferred\deferred.py”,第 129 行,运行中 引发永久任务失败(e) PermanentTaskFailure:“模块”对象没有属性“add_to_db”
I am trying to do this on Google App Engine in Python:
def add_to_db(person):
a = PersonDb(key_name = person)
# get some data
data1 = a.name
data2 = a.age
a.put()
for person in people:
deferred.defer(add_to_db, person, _queue="myque")
When I run the above code via a cron job it does not work. The file executes without error, The tasks (there are 200 strings in list 'People') get added to the queue correctly and trickle through correctly. But the database PersonDb does not get updated. I know the function works (the above is simplified) because this works:
for person in people:
add_to_db(person)
The above 2 lines of code works, and the database gets updated, but I need to run this as a deferred task. Any suggestions?
UPDATE: I am getting this in the log:
File "C:\Program Files\Google\google_appengine\google\appengine\ext\deferred\deferred.py", line 129, in run
raise PermanentTaskFailure(e)
PermanentTaskFailure: 'module' object has no attribute 'add_to_db'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况可能是因为您尝试推迟的函数 (
add_to_db
) 是在请求处理程序中定义的。将add_to_db
移动到另一个模块(不同的 python 文件)并从那里导入它(from myfuncs import add_to_db
)。延迟文章的限制部分提到了此限制。
This is probably happening because the function you are trying to defer (
add_to_db
) is defined in the request handler. Moveadd_to_db
to another module (a different python file) and import it from there (from myfuncs import add_to_db
).This limitation is mentioned in the limitations section of the deferred article.