用 Django 进行芹菜守护进程
Celery daemonization with Django
我是 Python 的新手,我真的很想边学边学。我有一个 Python 脚本,可以从不同站点提取数据并通过网站将其显示到 public。我在 Heroku 发行版中使用 Django。
我需要在早上自动 运行 我的脚本来更新信息。我看到芹菜是最好的选择。我现在有一个 celery 的工作示例,并且相信我已经用 Django 正确配置了它。但是,我还需要按照此处进行称为守护进程的过程:http://docs.celeryproject.org/en/latest/userguide/daemonizing.html 以便我的 celery worker 可以在后台运行。
这让我很困惑,找不到任何关于此教程的分步教程。我想我需要所有这些文件才能正常工作:
/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 选项放在这里吧?
您正在使用 Heroku,因此您不需要执行任何。只需将 celery 命令放入您的 Procfile 即可。
我是 Python 的新手,我真的很想边学边学。我有一个 Python 脚本,可以从不同站点提取数据并通过网站将其显示到 public。我在 Heroku 发行版中使用 Django。
我需要在早上自动 运行 我的脚本来更新信息。我看到芹菜是最好的选择。我现在有一个 celery 的工作示例,并且相信我已经用 Django 正确配置了它。但是,我还需要按照此处进行称为守护进程的过程:http://docs.celeryproject.org/en/latest/userguide/daemonizing.html 以便我的 celery worker 可以在后台运行。
这让我很困惑,找不到任何关于此教程的分步教程。我想我需要所有这些文件才能正常工作:
/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 选项放在这里吧?
您正在使用 Heroku,因此您不需要执行任何。只需将 celery 命令放入您的 Procfile 即可。