In Celery, how do I run a task, and then have that

2019-03-22 07:15发布

#tasks.py
from celery.task import Task
class Randomer(Task):
    def run(self, **kwargs):
        #run Randomer again!!!
        return random.randrange(0,1000000)


>>> from tasks import Randomer
>>> r = Randomer()
>>> r.delay()

Right now, I run the simple task. And it returns a random number. But, how do I make it run another task , inside that task?

2条回答
Emotional °昔
3楼-- · 2019-03-22 07:47

You can call other_task.delay() from inside Randomer.run; in this case you may want to set Randomer.ignore_result = True (and other_task.ignore_result, and so on).

Remember that celery tasks delay returns instantly, so if you don't put any limit or wait time on the nested calls (or recursive calls), you can reach meltdown pretty quickly.

Instead of recursion or nested tasks, you should consider an infinite loop to avoid stack overflow (no pun intended).

from celery.task import Task
class Randomer(Task):
    def run(self, **kwargs):
        while True:
           do_something(**kwargs)
           time.sleep(600)
查看更多
登录 后发表回答