我是Python新手,我真的很想边学边学。我有一个Python脚本,可以从不同的站点提取数据,并通过网站向公众显示。我正在使用Django和Heroku分布。
我需要在早上自动运行脚本来更新信息。我发现芹菜是最好的选择。我现在有一个芹菜的工作示例,并且相信我已经用Django正确地配置了它。但是,我还需要按照以下步骤完成一个称为守护化的过程:
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
文件配置正确。这是:芹菜。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))
和初始化。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选项放在这里,对吗?