#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?
You can chain subtasks as described here: http://docs.celeryproject.org/en/latest/userguide/canvas.html#chains
You can call
other_task.delay()
from insideRandomer.run
; in this case you may want to setRandomer.ignore_result = True
(andother_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).