如何使用Django配置Celery守护进程

时间:2015-11-06 05:20:59

标签: django celery virtualenv django-celery celeryd

据我所知,有两个文件描述了如何设置芹菜。有“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/celeryd由芹菜提供the default init.d script
  • /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启动芹菜时,它不起作用。相反,很明显它没有从我的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

任何有关捞丝无效的想法?我错过了什么专业吗?

2 个答案:

答案 0 :(得分:0)

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

celery worker -A my-proj

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

答案 1 :(得分:0)

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

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