Zou*_*uBi 7 python django daemon celery
按照本教程,我现在有一个Celery-Django应用程序,如果我用这个命令启动worker,它工作正常:celery -A myapp worker -n worker1.%h
在我的Django settings.py中,我为Celery设置了所有参数(消息代理的IP等等).一切都运作良好.
我现在的下一步是将此应用程序作为守护程序运行.所以我已经遵循了第二个教程,一切都很简单,除了现在,我没有加载settings.py中包含的Celery参数.例如,消息代理IP设置为127.0.0.1但在我的settings.py中,我将其设置为另一个IP地址.
在教程中,他们说:
确保定义Celery应用程序实例的模块还为DJANGO_SETTINGS_MODULE设置默认值,如Django的第一步中的Django项目示例所示.
所以我确定了.我在/ etc/default/celeryd这个:
导出DJANGO_SETTINGS_MODULE ="myapp.settings"
仍然没有工作...所以我也,在/etc/init.d/celeryd中有这一行,再次无法正常工作.我不知道该怎么办了.有人有线索吗?
编辑:这是我的celery.py:
from __future__ import absolute_import
import os
from django.conf import settings
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')
app = Celery('myapp')
# 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)
@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))
编辑#2:这是我的/ etc/default/celeryd:
# Names of nodes to start
#   most will only start one node:
CELERYD_NODES="worker1.%h"
# Absolute or relative path to the 'celery' command:
CELERY_BIN="/usr/local/bin/celery"
# App instance to use
# comment out this line if you don't use an app
CELERY_APP="myapp"
# Where to chdir at start.
CELERYD_CHDIR="/home/ubuntu/myapp-folder/"
# Extra command-line arguments to the worker
CELERYD_OPTS=""
# %N will be replaced with the first part of the nodename.
CELERYD_LOG_FILE="/var/log/celery/%N.log"
CELERYD_PID_FILE="/var/run/celery/%N.pid"
# Workers should run as an unprivileged user.
#   You need to create this user manually (or you can choose
#   a user/group combination that already exists, e.g. nobody).
CELERYD_USER="ubuntu"
CELERYD_GROUP="ubuntu"
# If enabled pid and log directories will be created if missing,
# and owned by the userid/group configured.
CELERY_CREATE_DIRS=1
# Name of the projects settings module.
export DJANGO_SETTINGS_MODULE=myapp.settings
export PYTHONPATH=$PYTHONPATH:/home/ubuntu/myapp-folder
这里的所有答案都可能是解决方案的一部分,但最后,它仍然无法正常工作.但我终于成功地使它发挥作用.
首先/etc/init.d/celeryd,我改变了这一行:
CELERYD_MULTI=${CELERYD_MULTI:-"celeryd-multi"}
通过:
CELERYD_MULTI=${CELERYD_MULTI:-"celery multi"}
第一个被标记为已弃用,可能是问题所在.
此外,我把它作为选项:CELERYD_OPTS =" - app = myapp"
并且不要忘记导出一些环境变量:
# Name of the projects settings module.
export DJANGO_SETTINGS_MODULE="myapp.settings"
export PYTHONPATH="$PYTHONPATH:/home/ubuntu/myapp-folder"
有了这一切,它现在正在我这边工作.