开发者

Is there something wrong with my Python code? (functions)

开发者 https://www.devze.com 2023-02-01 07:53 出处:网络
#tasks.py from celery.decorators import task @task() def add(x, y): add.delay(1, 9) return x + y >>> import tasks
#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 sho开发者_C百科uld continuously be tasks being added, due to this line: "add.delay(1,9)"

Note: I need each task to execute another 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 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.


You should look at subtasks and callbacks, might give you the answer you are looking for

http://celeryproject.org/docs/userguide/tasksets.html

0

精彩评论

暂无评论...
验证码 换一张
取 消