G
G
Grigory Dikiy2017-08-04 15:16:11
Django
Grigory Dikiy, 2017-08-04 15:16:11

Celery not starting tasks asynchronously?

Good afternoon. Setting up Django + Celery + RabbitMQ. But tasks are not executed asynchronously
by celery.py

import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'shop.settings')

app = Celery('shop')

# Configurations
app.config_from_object('django.conf:settings')
app.autodiscover_tasks()

__init__.py
from __future__ import absolute_import
from .celery import app as celery_app

__all__ = ['celery_app']

tasks.py
from celery import task
from django.core.mail import EmailMessage
from django.template.loader import get_template
from .models import Order


@task
def send_order_email(order_id):
    order = Order.objects.get(pk=order_id)
    template = get_template('order/email_order.html')
    html = template.render({'order': order})

    # Create & Send Email
    msg = EmailMessage(...)
    msg.content_subtype = 'html'
    msg.send()

How do I start everything:
RabbitMQ:
RabbitMQ 3.6.9. Copyright (C) 2007-2016 Pivotal Software, Inc.
  ##  ##      Licensed under the MPL.  See http://www.rabbitmq.com/
  ##  ##
  ##########  Logs: /usr/local/var/log/rabbitmq/[email protected]
  ######  ##        /usr/local/var/log/rabbitmq/[email protected]
  ##########
              Starting broker...
 completed with 10 plugins.

Celery worker
-------------- [email protected] v4.1.0 (latentcall)
---- **** -----
--- * ***  * -- Darwin-16.6.0-x86_64-i386-64bit 2017-08-04 15:01:56
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app:         shop:0x103ee29b0
- ** ---------- .> transport:   amqp://guest:**@localhost:5672//
- ** ---------- .> results:
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery


[tasks]
  . apps.orders.tasks.send_order_email

[2017-08-04 15:01:56,693: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
[2017-08-04 15:01:56,712: INFO/MainProcess] mingle: searching for neighbors
[2017-08-04 15:01:57,750: INFO/MainProcess] mingle: all alone
[2017-08-04 15:01:57,779: WARNING/MainProcess] /Users/dikiigr/MEGA/Dev/Work/PosudaHome/venv/lib/python3.5/site-packages/celery/fixups/django.py:202: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
  warnings.warn('Using settings.DEBUG leads to a memory leak, never '
[2017-08-04 15:01:57,779: INFO/MainProcess] [email protected] ready.

I test the task through Shell:
>>> from apps.orders.tasks import send_order_email
>>> send_order_email(20)

But in the worker there is no information about the task, the task itself is executed but synchronously. Launched Flower, but the result is the same (tasks are not executed asynchronously)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Tallmange, 2017-08-04
@frilix

>>> from apps.orders.tasks import send_order_email
>>> send_order_email.apply_async(20)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question