【问题标题】:Celery daemonization with Django用 Django 实现 Celery 守护进程
【发布时间】:2018-06-11 05:44:06
【问题描述】:

我是 Python 新手,我真的很想边学边学。我有一个 Python 脚本,它从不同的站点提取数据并通过网站向公众展示。我正在使用带有 Heroku 发行版的 Django。

我需要在早上自动运行我的脚本来更新信息。我看到芹菜是最好的选择。我现在有一个 celery 的工作示例,并且相信我已经用 Django 正确配置了它。但是,我还需要按照此处执行一个称为守护进程的过程:http://docs.celeryproject.org/en/latest/userguide/daemonizing.html,以便我的 celery 工人可以在后台运行。

这就是我难过的地方,找不到任何分步教程。我想我需要所有这些文件才能工作:

/etc/init.d/celeryd
/etc/defaults/celery
/etc/default/celerybeat
/project/celery.py
/project/__init__.py

所有这些文件都从根目录开始,manage.py 所在的位置 我相信我已经正确配置了 celery.py__init__.py 文件。它们是:celery.py

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery


# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')

app = Celery('project')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

和init.py:

from __future__ import absolute_import, unicode_literals

# 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

__all__ = ['celery_app']

我不确定如何正确配置其他两个文件。这是我为celeryd 提供的:

CELERYD_NODES=4
CELERY_BIN="project/celery"
CELERY_APP="project"
CELERYD_CHDIR="project/"

# Extra command-line arguments to the worker
#CELERYD_OPTS="--time-limit=300 --concurrency=8"
# Configure node-specific settings by appending node name to arguments:
#CELERYD_OPTS="--time-limit=300 -c 8 -c:worker2 4 -c:worker3 2 -Ofair:worker1"

# Set logging level to DEBUG
#CELERYD_LOG_LEVEL="DEBUG"

# %n will be replaced with the first part of the nodename.
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_PID_FILE="/var/run/celery/%n.pid"
CELERYD_USER="celery"
CELERYD_GROUP="celery"
CELERY_CREATE_DIRS=1

这里是celerybeat 文件:

# Absolute or relative path to the 'celery' command:
CELERY_BIN="project/celery"
#CELERY_BIN="/virtualenvs/def/bin/celery"

# App instance to use
# comment out this line if you don't use an app
CELERY_APP="project"
# or fully qualified:
#CELERY_APP="proj.tasks:app"

# Where to chdir at start.
CELERYBEAT_CHDIR="/project/"

# Extra arguments to celerybeat
#CELERYBEAT_OPTS="--schedule=/var/run/celery/celerybeat-schedule"

我也不确定我应该把下面这行放在哪里:

export DJANGO_SETTINGS_MODULE="settings"

我不确定/etc/defaults/celery 中的内容,或者我什至是否需要它。 我相信我还必须这样做,以便我的任务文件以某种方式具有CELERY_BEAT 选项的主文件。还没到这一步,但我应该可以把 c​​rontab 选项放在这里吗?

【问题讨论】:

    标签: django python-3.x heroku daemon django-celery


    【解决方案1】:

    您正在使用 Heroku,因此您无需执行任何这些操作。只需将 celery 命令放入您的 Procfile 中即可。

    【讨论】:

    • 非常感谢。没想过要检查 Heroku 的兼容性。
    猜你喜欢
    • 2014-11-24
    • 2017-06-11
    • 2016-02-07
    • 2016-10-07
    • 2018-11-03
    • 2014-09-01
    • 1970-01-01
    • 2016-03-31
    • 2018-07-27
    相关资源
    最近更新 更多