[英]Route celery task to specific queue
我的服務器上運行着兩個獨立的 celeryd 進程,由supervisor
。 它們被設置為監聽單獨的隊列:
[program:celeryd1]
command=/path/to/celeryd --pool=solo --queues=queue1
...
[program:celeryd2]
command=/path/to/celeryd --pool=solo --queues=queue2
...
我的 celeryconfig 看起來像這樣:
from celery.schedules import crontab
BROKER_URL = "amqp://guest:guest@localhost:5672//"
CELERY_DISABLE_RATE_LIMITS = True
CELERYD_CONCURRENCY = 1
CELERY_IGNORE_RESULT = True
CELERY_DEFAULT_QUEUE = 'default'
CELERY_QUEUES = {
'default': {
"exchange": "default",
"binding_key": "default",
},
'queue1': {
'exchange': 'queue1',
'routing_key': 'queue1',
},
'queue2': {
'exchange': 'queue2',
'routing_key': 'queue2',
},
}
CELERY_IMPORTS = ('tasks', )
CELERYBEAT_SCHEDULE = {
'first-queue': {
'task': 'tasks.sync',
'schedule': crontab(hour=02, minute=00),
'kwargs': {'client': 'client_1'},
'options': {'queue': 'queue1'},
},
'second-queue': {
'task': 'tasks.sync',
'schedule': crontab(hour=02, minute=00),
'kwargs': {'client': 'client_2'},
'options': {'queue': 'queue1'},
},
}
所有tasks.sync
任務必須路由到特定隊列(因此 celeryd 進程)。 但是當我嘗試使用sync.apply_async(kwargs={'client': 'value'}, queue='queue1')
手動運行任務時,兩個 celery 工作人員都接手了任務。 如何將任務路由到正確的隊列並且只能由綁定到隊列的工作人員運行?
您只運行一個 celerybeat 實例,對嗎?
也許您有與此沖突的舊隊列綁定? 嘗試運行rabbitmqctl list_queues
和rabbitmqctl list_bindings
,也許重置代理中的數據以從頭開始。
您在此處提供的示例應該有效,並且在我剛嘗試時對我有用。
提示:由於您使用與隊列名稱相同的 exchange 和 binding_key 值,因此您不必在 CELERY_QUEUES 中顯式列出它們。 當 CELERY_CREATE_MISSING_QUEUES 打開時(默認情況下),隊列將自動創建,就像您執行celeryd -Q queue1
或將任務發送到未定義的隊列一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.