How to make two tasks mutually exclusive in Celery?

♀尐吖头ヾ 提交于 2020-01-03 05:07:12

问题


Is there a way to disallow two different tasks to run simultaneously in Celery? I was thinking about defining a new queue with concurrency level=1, and send those tasks to that queue, but I couldn't find an example. Is that possible?

Thanks!


回答1:


Yes, if you don't need to worry about overall throughput it is possible to create a separate queue and have a dedicated worker with concurrency set to 1. You can create as many queues as you want and configure which of those queues each worker receives messages from.

When starting the worker, you can pass the -Q parameter to set its queues and the -c parameter to set how many threads it uses, as discussed in the Queues and Concurrency sections of the Workers Guide.

celery -A my_project worker -l info -Q queue1 -c 1

Then you can setup global mappings that define which queues each task goes to using the Routing Guide.

CELERY_ROUTES = { 'my_app.tasks.task1': {'queue': 'queue1'}, 'my_app.tasks.task2': {'queue': 'queue2'}, }

Alternatively, you can specify the queue at the time you submit each task instance based on the Calling Tasks Guide.

task1.apply_async(queue='queue1')



来源:https://stackoverflow.com/questions/28670524/how-to-make-two-tasks-mutually-exclusive-in-celery

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