A
A
Art0052020-11-22 20:19:32
Django
Art005, 2020-11-22 20:19:32

Why doesn't celery in django run asynchronously?

I tried to make for myself an asynchronous sending of letters to django through Celery. I started everything according to the tutorial, I decided to put n seconds of sleep in the task and I look at the response from the server itself comes in n seconds, but the task itself works out. It seems that I tried to correct what I found where, but the problem did not disappear.

settings.py

CELERY_BROKER_URL = 'redis://localhost'

CELERY_ACCEPT_CONTENT = ['application/json']

CELERY_TASK_SERIALIZER = 'json'

CELERY_RESULT_BACKEND = 'redis://localhost:6379'

BROKER_URL = 'redis://localhost:6379'

CELERY_RESULT_BACKEND = 'redis://localhost:6379'

CELERY_RESULT_SERIALIZER = 'json'


celery.py


import os

from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'send_mails.settings')
app = Celery('send_mails')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


tasks (simplified)
import time
from celery import shared_task

@shared_task
def sleep():
    time.sleep(5)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Art005, 2020-11-22
@Art005

Called the function directly, not through delay. Spent about 4 hours to find this error...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question