【问题标题】:How to configure Celery Daemon with Django如何使用 Django 配置 Celery 守护进程
【发布时间】:2016-02-07 04:49:00
【问题描述】:

据我所知,有两个文档描述了如何设置 celery。有“Running the worker as a daemon”和“First steps with Django”。

在 Django 文档中,它说:

我们还添加了 Django 设置模块作为 Celery 的配置源。这意味着您不必使用多个配置文件,而是直接从 Django 设置中配置 Celery。

听起来很棒。但是,据我所知,这些是完整的 Celery 守护进程所需的文件:

  • /etc/init.d/celeryd
  • /etc/defaults/celery
  • /my-proj/celery.py
  • /my-proj/__init__.py

还有可能:

  • /my-proj/settings.py

文件太多了。我想我已经把它们都设置好了:

  • /etc/init.d/celerydthe default init.d script 由 celery 提供。
  • /etc/defaults/celery 几乎一无所有。只是指向我的应用程序的指针:

    export DJANGO_SETTINGS_MODULE='cl.settings'
    
  • /my-proj/celery.py 有来自First Steps with Django 的推荐文件:

    from __future__ import absolute_import
    
    import os
    
    from celery import Celery
    
    # set the default Django settings module for the 'celery' program.
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
    
    from django.conf import settings  # noqa
    
    app = Celery('proj')
    
    # Using a string here means the worker will not have to
    # pickle the object when using Windows.
    app.config_from_object('django.conf:settings')
    app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
    
  • /my-proj/__init__.py 有来自First Steps with Django 的推荐代码:

    from __future__ import absolute_import
    
    # This will make sure the app is always imported when
    # Django starts so that shared_task will use this app.
    from .celery import app as celery_app
    

我的settings.py 文件中有所有与芹菜相关的设置,如下所示:

CELERY_BIN = '/var/www/.virtualenvs/my-env/bin/celery'
CELERYD_USER = 'www-data'
CELERYD_GROUP = 'www-data'
CELERYD_CONCURRENCY = 20
BROKER_URL = 'redis://'
BROKER_POOL_LIMIT = 30

然而,当我使用 sudo service celeryd start 启动 celery 时,它不起作用。相反,很明显它没有从我的 Django 项目中获取我的设置,因为它说:

Nov 05 20:51:59 pounamu celeryd[30190]: celery init v10.1.
Nov 05 20:51:59 pounamu celeryd[30190]: Using config script: /etc/default/celeryd
Nov 05 20:51:59 pounamu celeryd[30190]: No passwd entry for user 'celery'
Nov 05 20:51:59 pounamu su[30206]: No passwd entry for user 'celery'
Nov 05 20:51:59 pounamu su[30206]: FAILED su for celery by root
Nov 05 20:51:59 pounamu su[30206]: - ??? root:celery
Nov 05 20:51:59 pounamu systemd[1]: celeryd.service: control process exited, code=exited status=1

有什么想法可以解决吊索不工作的问题吗?我错过了什么重要的东西吗?

【问题讨论】:

  • 您找到解决方案了吗?面临同样的问题
  • 是的,我确定我成功了,但是不,我不记得缺少的拼图是什么。已经有好几个版本了,很多年了。

标签: django celery virtualenv django-celery celeryd


【解决方案1】:

您正试图以系统用户“celery”的身份运行 celery,这是 init 脚本默认使用的。您应该创建此用户,或者您可以通过在 /etc/defaults/celery 中设置 CELERYD_USER 来覆盖它。

我个人更喜欢使用supervisord 来管理芹菜。

【讨论】:

  • 我在我的 django 设置中设置 CELERY_USER。问题是这些设置没有被拾取。
【解决方案2】:

您缺少CELERY_APP 设置。将其设置在configuration file/etc/defaults/celery 或作为worker 命令的参数:

celery worker -A my-proj

否则,Celery 不知道它应该查看/my-proj/celery.py。 django 环境变量不会影响 Celery 加载的内容。

【讨论】:

    猜你喜欢
    • 2018-06-11
    • 2014-11-24
    • 2016-03-31
    • 2014-09-01
    • 2017-06-11
    • 2016-10-07
    • 2021-02-03
    • 1970-01-01
    • 2018-11-03
    相关资源
    最近更新 更多