How to register Celery task to specific worker?

一笑奈何 提交于 2021-01-04 07:19:22

问题


I am developing web application in Python/Django, and I have several tasks which are running in celery.

I have to run task A one at a time so I have created worker with --concurrency=1 and routed task A to that worker using following command.

celery -A proj worker -Q A -c 1 -l INFO

Everything is working fine as this worker handle task A and other tasks are routed to default queue.

But, above worker return all task when I use inspect command to get registered task for worker. That is absolutely true because when I start worker, it displays all tasks of projects as registered task but handle only task A.

Following is the output of worker when I start it.

$ celery -A proj worker -Q A -c 1 -l INFO

 -------------- celery@pet_sms v4.0.2 (latentcall)
---- **** ----- 
--- * ***  * -- Linux-4.8.10-040810-generic-x86_64-with-Ubuntu-16.04-xenial 2018-04-26 14:11:49
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         proj:0x7f298a10d208
- ** ---------- .> transport:   redis://localhost:6379/0
- ** ---------- .> results:     disabled://
- *** --- * --- .> concurrency: 1 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> A exchange=A(direct) key=A


[tasks]
  . task_one
  . task_two
  . task_three
  . A
  . task_four
  . task_five

Is there any way to register specific task to the worker in celery?


回答1:


Notice following two parts in your worker log.

[queues]
  .> A exchange=A(direct) key=A

[tasks]
  . task_one
  . task_two
  . task_three
  . A
  . task_four
  . task_five

The first part [queues] shows the queues your worker consumes.

And it shows A, exchange=A(direct), key=A, indicating this worker only consumes tasks that are from the queue A. Which is exactly what you want. And you were achieving this effect because you specified -Q A when you started the worker by the command $ celery -A proj worker -Q A -c 1 -l INFO.

The second part [tasks] shows all the registered tasks of this app.

Though other tasks such as task_one task_five are all registered, since these tasks do not go into queue A, therefore this worker does not consume the tasks task_one task_five.



来源:https://stackoverflow.com/questions/50040495/how-to-register-celery-task-to-specific-worker

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