我的Python代码有问题吗? (功能)

发布于 2024-10-09 09:50:39 字数 377 浏览 0 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

混浊又暗下来 2024-10-16 09:50:39

据我所知,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.

倾其所爱 2024-10-16 09:50:39

函数体内的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, subclass Task instead of using decorator and you'll be able to use recursion without a problem.

孤君无依 2024-10-16 09:50:39

您应该查看子任务和回调,可能会给您正在寻找的答案

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文