我正在尝试从Celery文档运行一个基本示例,但是当我运行'来自任务导入添加'时,它给出了一个错误,说'找不到模块'。
这些是我更改的文件。
凸出/凸出/ celery.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))
凸出/凸出/ 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',
)
凸出/凸出/的初始化的.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)
感谢任何建议。谢谢!
答案 0 :(得分:1)
我遇到了同样的问题,
我在应用中的任务未注册。
我可以通过将celery.py导入到settings.py而不是init.py中来解决这个问题
凸出/凸出/ init.py
from __future__ import absolute_import
# is empty
凸出/凸出/ settings.py
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',
)
答案 1 :(得分:-3)
如果其他人有同样的问题。在shell中执行此操作。 myapp是应用程序的名称,在我的例子中它是proj。
require