与Django的芹菜守护进程

问题描述 投票:0回答:1

我是Python的新手,并且在我去的时候真的想学习东西。我有一个Python脚本,可以从不同的站点提取数据,并通过网站向公众显示。我正在使用带有Heroku发行版的Django。

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

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

/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选项的主文件。还没有到目前为止,但我应该能够把crontab选项放在这里吗?

django python-3.x heroku daemon django-celery
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.