Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么可以';芹菜守护进程看到任务了吗?_Python_Django_Debian_Celery - Fatal编程技术网

Python 为什么可以';芹菜守护进程看到任务了吗?

Python 为什么可以';芹菜守护进程看到任务了吗?,python,django,debian,celery,Python,Django,Debian,Celery,我有一个Django 1.62应用程序在Debian 7.8上运行,Nginx 1.2.1作为我的代理服务器,Gunicorn 19.1.1作为我的应用服务器。我已经安装了芹菜3.1.7和RabbitMQ 2.8.4来处理异步任务。我可以启动芹菜工人作为守护进程,但每当我尝试运行芹菜文档中所示的测试“添加”任务时,我会出现以下错误: Received unregistred task of type u'apps.photos.tasks.add'. The message has been i

我有一个Django 1.62应用程序在Debian 7.8上运行,Nginx 1.2.1作为我的代理服务器,Gunicorn 19.1.1作为我的应用服务器。我已经安装了芹菜3.1.7和RabbitMQ 2.8.4来处理异步任务。我可以启动芹菜工人作为守护进程,但每当我尝试运行芹菜文档中所示的测试“添加”任务时,我会出现以下错误:

Received unregistred task of type u'apps.photos.tasks.add'.
The message has been ignored and discarded.

Traceback (most recent call last):
File "/home/swing/venv/swing/local/lib/python2.7/site-packages/celery/worker/consumer.py", line 455, in on_task_received
strategies[name](message, body,
KeyError: u'apps.photos.tasks.add'
我的所有配置文件都保存在一个“conf”目录中,该目录位于我的“myproj”项目目录的正下方。“添加”任务位于apps/photos/tasks.py中

myproj
│
├── apps
    ├── photos
    │   ├── __init__.py
    │   ├── tasks.py
    conf
    ├── celeryconfig.py
    ├── celeryconfig.pyc
    ├── celery.py
    ├── __init__.py
    ├── middleware.py
    ├── settings
    │   ├── base.py
    │   ├── dev.py
    │   ├── __init__.py
    │   ├── prod.py
    ├── urls.py
    ├── wsgi.py
以下是任务文件:

# apps/photos/tasks.py
from __future__ import absolute_import
from conf.celery import app

@app.task
def add(x, y):
    return x + y
以下是我的芹菜应用程序和配置文件:

# conf/celery.py
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
from conf import celeryconfig

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'conf.settings')
app = Celery('conf')
app.config_from_object(celeryconfig)
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

# conf/celeryconfig.py
BROKER_URL = 'amqp://guest@localhost:5672//'
CELERY_RESULT_BACKEND = 'amqp'
CELERY_ACCEPT_CONTENT = ['json', ]
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
这是我的芹菜守护程序配置文件。我注释掉了芹菜应用程序,因为我发现,如果我取消对芹菜守护进程的注释,它甚至不会启动。我还发现需要将“-config”参数添加到CELERYD_OPTS中,以便启动守护进程。我创建了一个可以写入日志和pid文件的非特权“芹菜”用户

# /etc/default/celeryd
CELERYD_NODES="worker1"
CELERYD_LOG_LEVEL="DEBUG"
CELERY_BIN="/home/myproj/venv/myproj/bin/celery"
#CELERY_APP="conf"
CELERYD_CHDIR="/www/myproj/"
CELERYD_OPTS="--time-limit=300 --concurrency=8 --config=celeryconfig"
CELERYD_LOG_FILE="/var/log/celery/%N.log"
CELERYD_PID_FILE="/var/run/celery/%N.pid"
CELERYD_USER="celery"
CELERYD_GROUP="celery"
CELERY_CREATE_DIRS=1
我可以从日志文件中看到,当我运行命令“sudo-service-celeryd-start”时,芹菜没有任何错误地启动。但是,如果打开pythonshell并运行以下命令,我将看到我在开头描述的错误

$ python shell
In [] from apps.photos.tasks import add
In [] result = add.delay(2, 2)
有趣的是,如果我检查Cellery的registered tasks对象,该任务将列出:

In [] import celery
In [] celery.registry.tasks

Out [] {'celery.chain': ..., 'apps.photos.tasks.add': <@task: apps.photos.tasks.add of conf:0x16454d0> ...}
[]中的
导入芹菜
在[]芹菜.registry.tasks中
Out[]{'celery.chain':…,'apps.photos.tasks.add':…}
这里的其他类似问题已经讨论过使用PYTHONPATH环境变量,而我没有这样的变量。我一直不知道如何设置PYTHONPATH,这个项目在没有它的情况下已经运行了一年多

我还应该补充一点,我的生产设置文件是conf/settings/prod.py。它从base.py导入我的所有基本(独立于层)设置,并添加一些额外的与生产相关的设置

谁能告诉我我做错了什么?我已经为这个问题挣扎了三天了


谢谢

看起来这是由于相对导入错误造成的

>>> from project.myapp.tasks import mytask
>>> mytask.name
'project.myapp.tasks.mytask'

>>> from myapp.tasks import mytask
>>> mytask.name
'myapp.tasks.mytask'
如果使用相对导入,则应显式设置名称

@task(name='proj.tasks.add')
def add(x, y):
   return x + y

签出:

我正在使用芹菜4.0.2和django,我创建了一个芹菜用户和组,用于芹菜D,并遇到了同样的问题。命令行版本运行良好,但celeryd没有注册任务。这不是一个相对的命名问题


解决方案是将芹菜用户添加到可以访问django项目的组中。在我的例子中,此组是www数据,具有读取、执行和不写入功能。

my tasks.py文件位于apps.photos.tasks中,因此我将add方法的装饰器更改为“@app.task(name='apps.photos.tasks.add')但是我仍然得到了关键错误。如果你用
-l info
选项启动你的工作人员,你的任务名称是否在开始时显示?不,我也尝试过,并注意到没有显示任何任务。我已经开始构建一个test Django应用程序,每次都在阅读芹菜文档,并尝试不同的方法来尝试了解其中的位置问题出在我的配置中。现在已经五天了。这是我在没有找到解决方案的情况下处理问题的时间最长的一次。我现在试图解决的问题是Initd celeryd脚本如何知道项目中的哪个文件包含行“app=cellery('tasks'))"? 在我的test Django项目中,这一行位于文件myproj/conf/tasks.py中(其中myproj是项目的根目录,我的其他设置文件如settings.py、url.py和wsgi.py位于“conf”中)子目录。如果我的celeryd文件中没有Celery_应用程序,芹菜就可以开始了。celeryd如何知道芹菜应用程序的定义位置?@Robert你是如何“找到”的?答案是什么。我知道这很旧,但非常感谢你的帮助。使用此: