gpt4 book ai didi

python - 将 celery 任务路由到特定队列

转载 作者:太空狗 更新时间:2023-10-30 01:03:15 25 4
gpt4 key购买 nike

我的服务器上运行着两个独立的 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 worker 都接手了任务。我怎样才能使任务路由到正确的队列,并且只能由绑定(bind)到队列的工作人员运行?

最佳答案

您只运行了一个 celerybeat 实例,对吗?

也许您有与此冲突的旧队列绑定(bind)?尝试运行 rabbitmqctl list_queuesrabbitmqctl list_bindings,可能会重置代理中的数据以从头开始。

您在此处提供的示例应该有效,并且在我刚刚尝试时对我有效。

提示:由于您使用与队列名称相同的交换和绑定(bind)键值,您不必在 CELERY_QUEUES 中明确列出它们。当 CELERY_CREATE_MISSING_QUEUES打开(默认情况下)队列将自动创建,就像您拥有的一样如果您只是执行 celeryd -Q queue1 或将任务发送到未定义的队列。

关于python - 将 celery 任务路由到特定队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10079816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com