芹菜和兔子MQ Django

时间:2017-11-14 17:36:10

标签: django ubuntu rabbitmq celery

我使用命令安装了Celery和RabbitMQ: def classify_image(request): if request.method == 'POST' and request.FILES['test_image']: test_image = request.FILES['test_image'] test_image = cv2.imread(test_image) test_image = cv2.resize(test_image, (128, 128)) test_image = np.array(test_image) test_image = test_image.astype('float32') test_image /= 255 print(test_image.shape) test_image = np.expand_dims(test_image, axis=0) pred = model.predict_classes(test_image) print(pred) return JsonResponse(pred, safe=False) 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'

我不明白该怎么办?

tasks.py

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'

celery.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 {},\n\nYou 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

初始化的.py

<。>从.celery导入应用程序as celery_app

GitHub上的这个项目https://github.com/Loctarogar/Django-by-Example

2 个答案:

答案 0 :(得分:0)

导入芹菜应用的__init__.py文件在哪里?它在shop/myshop/目录中吗?所以它需要在myshop应用程序内部才能在启动时加载芹菜应用程序。

答案 1 :(得分:0)

尝试在celery.py所在的项目目录中仔细检查文件settings.py的拼写

或者您可以按照THIS文档了解如何使用带有Django的芹菜来获取更多解释

您还可以查看this post