问题
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