How to start a task only when all other tasks have finished in Celery

北城以北 提交于 2019-12-04 05:51:59

问题


In Celery, I want to start a task only when all the other tasks have completed. I found some resources like this one : Celery Starting a Task when Other Tasks have Completed and Running a task after all tasks have been completed But I am quite new to celery and could not really understand the above (or many other resources for that matter).

So I have defined a task as so in a tasks.py:

@celapp.task()
def sampleFun(arg1, arg2, arg3):
    # do something here

and I call it as this :

for x in xrange(4):    
   tasks.sampleFun.delay(val1, val2, val3)

And I assume that there would be 4 different tasks created. This actually happens as I can see the same on the web interface of Celery Flower.

Now what I would like to do is, have another task added. Say finalTask that should start only once all the above 4 tasks have completed their execution.

I also read some doc on groups and chords in Celery but it says that if I want to do it I need to group my tasks together so they get executed in parallel. I don't want to do that. (Might be a good idea, but right now my goal is to be able to understand things. So am not really focussed on performance etc. at this point in time)

How to do this? Another more basic question which might sound rather stupid is : When we say tasks.sampleFun.delay does it create asynchronous tasks or not ?

来源:https://stackoverflow.com/questions/46976009/how-to-start-a-task-only-when-all-other-tasks-have-finished-in-celery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!