【问题标题】:Django celery run multiple workers with different queuesDjango celery 运行多个具有不同队列的工作人员
【发布时间】:2020-01-28 23:52:46
【问题描述】:

我尝试在 django 中为 celery 配置三个队列/worker。

settings.py

CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Europe/Berlin'
CELERY_QUEUES = (
    Queue('manually_task', Exchange('manually_task'), routing_key='manually_task'),
    Queue('periodically_task', Exchange('periodically_task'), routing_key='periodically_task'),
    Queue('firsttime_task', Exchange('firsttime_task'), routing_key='firsttime_task'),
)
CELERY_ROUTES = {
    'api.tasks.manually_task': {
        'queue': 'manually_task',
        'routing_key': 'manually_task',
    },
    'api.tasks.periodically_task': {
        'queue': 'periodically_task',
        'routing_key': 'periodically_task',
    },
    'api.tasks.firsttime_task': {
        'queue': 'firsttime_task',
        'routing_key': 'firsttime_task',
    },
}

我有三个任务,每个任务都应该有自己的队列/工作者。 我的任务如下所示:

@shared_task
def manually_task(website_id):
    print("manually_task");
    website = Website.objects.get(pk=website_id)
    x = Proxy(website, "49152")
    x.startproxy()
    x = None


@periodic_task(run_every=(crontab(hour=19, minute=15)), ignore_result=True)
def periodically_task():
    websites = Website.objects.all()

    for website in websites:
        x = Proxy(website, "49153")
        x.startproxy()
        x = None


@shared_task
def firsttime_task(website_id):
    website = Website.objects.get(pk=website_id)
    x = Proxy(website, "49154")
    x.startproxy()
    x = None

现在第一次试用我只启动一个工人:

celery -A django-proj worker -Q manually_task -n manually_task

我的问题是任务显然没有执行,“manually_task”没有打印。 为什么它不起作用?

【问题讨论】:

  • 在worker启动后你是如何调用你的任务的?
  • 在这样的视图中:manually_task.delay(webseite.pk)
  • 只有一名工作人员且在settings.py 中没有CELERY_QUEUESCELERY_ROUTES 设置,它工作正常。

标签: django celery django-celery


【解决方案1】:

根据评论,我建议您在从manually_task.apply_async((webseite.pk,), queue='manually_task') 之类的视图调用任务时提供队列名称,或者在启动工作程序时添加名为celery 的默认队列,如celery -A django-proj worker -Q manually_task,celery 中所示

【讨论】:

    猜你喜欢
    • 2013-02-18
    • 2014-05-29
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多