我的Python代码有问题吗? (功能)
#tasks.py
from celery.decorators import task
@task()
def add(x, y):
add.delay(1, 9)
return x + y
>>> import tasks
>>> res = tasks.add.delay(5, 2)
>>> res.result()
7
如果我运行此代码,我希望任务不断添加到队列中。但事实并非如此!仅第一个任务 (5,2) 被添加到队列中并进行处理。
由于这一行,应该不断添加任务:“add.delay(1,9)”
注意:我需要每个任务执行另一个任务。
#tasks.py
from celery.decorators import task
@task()
def add(x, y):
add.delay(1, 9)
return x + y
>>> import tasks
>>> res = tasks.add.delay(5, 2)
>>> res.result()
7
If I run this code, I expect tasks to be continously added to the queue. But it's not! Only the first task (5,2) gets added to the queue and processed.
There should continuously be tasks being added, due to this line: "add.delay(1,9)"
Note: I need each task to execute another task.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,periodic_task 装饰器正在创建 preiodic 任务,task 仅创建一个任务。而延迟只是异步执行它。
您应该只使用periodic_task,而不是递归。
As far as I can see, a periodic_task decorator is creating preiodic tasks, task creates just one task. And delay just executes it asynchronically.
You should just use periodic_task, instead of recursion.
函数体内的
add
指的是原始函数,而不是它的修饰版本。如果您只需要重复运行任务,请改用
@periodic_task
。仅当每次延迟不同时才需要递归。在这种情况下,子类Task
而不是使用装饰器,您将能够毫无问题地使用递归。add
inside function body refers to original function, not its decorated version.If you just need to run task repeatedly, use
@periodic_task
instead. You only need recursion if delay is different each time. In this case, subclassTask
instead of using decorator and you'll be able to use recursion without a problem.您应该查看子任务和回调,可能会给您正在寻找的答案
http://celeryproject。 org/docs/userguide/tasksets.html
You should look at subtasks and callbacks, might give you the answer you are looking for
http://celeryproject.org/docs/userguide/tasksets.html