我用以下命令安装了Celery和RabbitMQ:pip install celery
和sudo apt-get install rabbitmq
. 芹菜不起作用;我收到此错误:
Traceback (most recent call last):
File "/home/morilon/dj/shop/lib/python3.5/site-
packages/celery/app/utils.py", line 361, in find_app
found = sym.app
AttributeError: module 'myshop' has no attribute 'app'
在处理上述异常期间,发生了另一个异常:
Traceback (most recent call last):
File "/home/morilon/dj/shop/bin/celery", line 11, in <module>
sys.exit(main())
File "/home/morilon/dj/shop/lib/python3.5/site-packages/celery/__main__.py", line 14, in main
_main()
File "/home/morilon/dj/shop/lib/python3.5/site-packages/celery/bin/celery.py", line 326, in main
cmd.execute_from_commandline(argv)
File "/home/morilon/dj/shop/lib/python3.5/site-packages/celery/bin/celery.py", line 488, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/home/morilon/dj/shop/lib/python3.5/site-packages/celery/bin/base.py", line 279, in execute_from_commandline
argv = self.setup_app_from_commandline(argv)
File "/home/morilon/dj/shop/lib/python3.5/site-packages/celery/bin/base.py", line 481, in setup_app_from_commandline
self.app = self.find_app(app)
File "/home/morilon/dj/shop/lib/python3.5/site-packages/celery/bin/base.py", line 503, in find_app
return find_app(app, symbol_by_name=self.symbol_by_name)
File "/home/morilon/dj/shop/lib/python3.5/site-packages/celery/app/utils.py", line 366, in find_app
found = sym.celery
AttributeError: module 'myshop' has no attribute 'celery'
我不明白该怎么办?
tasks.py
from celery import task
from django.core.mail import send_mail
from .models import Order
@task
def order_created(order_id):
"""
Task to send an e-mail notification when an order is successfully created.
"""
order = Order.objects.get(id=order_id)
subject = 'Order nr. {}'.format(order.id)
message = 'Dear {},nnYou have successfully placed an order. Your order id is {}.'.format(order.first_name,
order.id)
mail_sent = send_mail(subject, message, 'admin@myshop.com', [order.email])
return mail_sent
celery.py
import os
from celery import Celery
from django.conf import settings
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myshop.settings')
app = Celery('myshop')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
初始化.py
从 .celery 导入应用程序作为celery_app
GitHub 上的这个项目 https://github.com/Loctarogar/Django-by-Example
尝试仔细检查settings.py
所在的项目目录中文件celery.py
的拼写
或者你可以按照这个文档在 Django 中使用芹菜来获得更多解释
你也可以看看这篇文章!
导入芹菜应用程序的__init__.py
文件在哪里?它是否在shop/myshop/
目录中?因此,它需要在myshop应用程序中,以便在启动时加载芹菜应用程序。