Python 芹菜Django未注册任务,shell中未找到模块错误
我试图从芹菜文档中运行一个基本示例,但是当我运行“fromtasks import add”时,会出现一个错误,显示“module is not found” 这些是我更改的文件 项目/项目/芹菜.pyPython 芹菜Django未注册任务,shell中未找到模块错误,python,django,registry,task,celery,Python,Django,Registry,Task,Celery,我试图从芹菜文档中运行一个基本示例,但是当我运行“fromtasks import add”时,会出现一个错误,显示“module is not found” 这些是我更改的文件 项目/项目/芹菜.py from __future__ import absolute_import import os from celery import Celery # set the default Django settings module for the 'celery' program. os.
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))
proj/proj/settings.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',
)
from .celery import app as celery_app
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/init.py
from __future__ import absolute_import
from .celery import app as celery_app
proj/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)
感谢您的建议。谢谢大家! 如果其他人也有同样的问题。在shell中执行此操作。myapp是应用程序的名称,在我的例子中是proj
from myapp.tasks import add
我也有同样的问题 应用程序中的我的任务未注册 我可以通过将芹菜.py的导入移到settings.py而不是init.py来解决这个问题 项目/proj/init.py
from __future__ import absolute_import
# is empty
proj/proj/settings.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',
)
from .celery import app as celery_app
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',
)
这会有什么帮助?请你再澄清一下好吗。