I'm using Celery 3.1. I need to only execute the next task when the last one is finish. How can I assure that there are not two tasks working at the same time? I've read the documentation but it is not clear for me.
我用芹菜3.1。我只需要在最后一个任务完成时执行下一个任务。我如何保证没有两个任务同时工作?我看过文件,但我不清楚。
I've the following scheme:
我以下方案:
Task Main
- Subtask 1
- Subtask 2
I need that when I call "Task Main" the process will run till the end(Subtask 2) without any new "Task Main" starting.
我需要当我调用“Task Main”时,进程将运行到结束(Subtask 2),而不会启动任何新的“任务主”。
How can I assure this?
我怎么能保证呢?
2 个解决方案
#1
1
If I understand you want to execute only MainTask one by one, and you want to call subtasks in your MainTask. Without creating separate queues and at least 2 separate workers this is impossible. Because if you will store in same queue all tasks looks for celery as same tasks.
如果我理解您想要逐个执行主任务,并且您想要在主任务中调用子任务。如果不创建单独的队列和至少两个独立的工作人员,这是不可能的。因为如果你将所有任务都存储在相同的队列中,那么所有任务都将寻找芹菜作为相同的任务。
So solution for is:
所以解决方案是:
- map MainTask to main_queue
- 地图MainTask main_queue
- Start separate worker for this queue like: celeryd --concurrency=1 --queue=main_queue
- 为这个队列启动单独的worker,比如:celeryd——concurrent =1——queue=main_queue
- map subtasks to sub_queue
- 子任务映射到sub_queue
- Start separate worker for this queue celeryd --queue=sub_queue
- 为这个队列启动单独的worker celeryd——queue=sub_queue
Should work!
应该工作!
But I think this is complecated architecture, may be you can make it much easier if you will redesign your process.
但是我认为这是一个互补的架构,如果你重新设计你的流程,你可以使它变得更容易。
Also you can find this useful (it works for you but it could run parallel MainTask): You should try to use chains, here is an example on Celery's docs: http://docs.celeryproject.org/en/latest/userguide/tasks.html#avoid-launching-synchronous-subtasks.
您还可以发现这很有用(它适用于您,但可以运行并行主任务):您应该尝试使用链,这里有一个关于芹菜文档的示例:http://docs.celeryproject.org/en/latest/userguide/tasks.html#avoid-launching-synchronous-subtasks。
#2
0
One strategy is through the use of locks. The Celery Task Cookbook has an example at http://docs.celeryproject.org/en/latest/tutorials/task-cookbook.html.
一种策略是使用锁。芹菜任务食谱有一个例子,http://docs.celeryproject.org/en/latest/tutorials/task-cookbook.html。
#1
1
If I understand you want to execute only MainTask one by one, and you want to call subtasks in your MainTask. Without creating separate queues and at least 2 separate workers this is impossible. Because if you will store in same queue all tasks looks for celery as same tasks.
如果我理解您想要逐个执行主任务,并且您想要在主任务中调用子任务。如果不创建单独的队列和至少两个独立的工作人员,这是不可能的。因为如果你将所有任务都存储在相同的队列中,那么所有任务都将寻找芹菜作为相同的任务。
So solution for is:
所以解决方案是:
- map MainTask to main_queue
- 地图MainTask main_queue
- Start separate worker for this queue like: celeryd --concurrency=1 --queue=main_queue
- 为这个队列启动单独的worker,比如:celeryd——concurrent =1——queue=main_queue
- map subtasks to sub_queue
- 子任务映射到sub_queue
- Start separate worker for this queue celeryd --queue=sub_queue
- 为这个队列启动单独的worker celeryd——queue=sub_queue
Should work!
应该工作!
But I think this is complecated architecture, may be you can make it much easier if you will redesign your process.
但是我认为这是一个互补的架构,如果你重新设计你的流程,你可以使它变得更容易。
Also you can find this useful (it works for you but it could run parallel MainTask): You should try to use chains, here is an example on Celery's docs: http://docs.celeryproject.org/en/latest/userguide/tasks.html#avoid-launching-synchronous-subtasks.
您还可以发现这很有用(它适用于您,但可以运行并行主任务):您应该尝试使用链,这里有一个关于芹菜文档的示例:http://docs.celeryproject.org/en/latest/userguide/tasks.html#avoid-launching-synchronous-subtasks。
#2
0
One strategy is through the use of locks. The Celery Task Cookbook has an example at http://docs.celeryproject.org/en/latest/tutorials/task-cookbook.html.
一种策略是使用锁。芹菜任务食谱有一个例子,http://docs.celeryproject.org/en/latest/tutorials/task-cookbook.html。