芹菜Django未注册任务,在sh中找不到模块错误

2024-04-25 22:21:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从芹菜文档中运行一个基本示例,但是当我运行“from tasks import add”时,会出现一个错误,说“找不到模块”。

这些是我换的文件。

项目/项目/芹菜.py

from __future__ import absolute_import

import os

from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')

from django.conf import settings

app = Celery('proj')

# 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))

项目/项目/设置.py

BROKER_URL = 'amqp://guest:guest@localhost//'

#: Only add pickle to this list if your broker is secured
#: from unwanted access (see userguide/security.html)
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'


INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'celerymod',
    'djcelery',
)

proj/proj/初始化.py

from __future__ import absolute_import
from .celery import app as celery_app

项目/celerymod/tasks.py

from __future__ import absolute_import

from celery import shared_task


@shared_task
def add(x, y):
    return x + y


@shared_task
def mul(x, y):
    return x * y


@shared_task
def xsum(numbers):
    return sum(numbers)

谢谢你的建议。谢谢您!


Tags: the项目djangofrompyimportapptask