【发布时间】:2014-09-01 18:24:32
【问题描述】:
通过关注this tutorial,我现在有了一个 Celery-Django 应用程序,如果我使用以下命令启动工作程序,它就可以正常工作: celery -A myapp worker -n worker1.%h
在我的 Django settings.py 中,我为 Celery 设置了所有参数(消息代理的 IP 等...)。一切正常。
我现在的下一步是将这个应用程序作为守护程序运行。所以我关注了this second tutorial,一切都很简单,除了现在,我的settings.py中包含的Celery参数没有加载。例如,消息代理 IP 设置为 127.0.0.1,但在我的 settings.py 中,我将其设置为其他 IP 地址。
在教程中,他们说:
确保定义 Celery 应用实例的模块也为 DJANGO_SETTINGS_MODULE 设置默认值,如 First steps with 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
【问题讨论】:
-
你的 celery.py 文件是什么样子的?
-
你可以在我的第一篇文章中看到 celery.py。你有什么想法吗?
-
你在 /etc/default/celeryd 中有这行 [CELERYD_CHDIR="/some_dir/myapp/"] 吗?
-
我已经有好几年没有尝试配置 Celery 了。我真的希望它变得更好,但看起来它和往常一样可怕。
标签: python django celery daemon