Skip to content

Q&A · Answered

How to configure Celery with Django

asked 9 months, 1 week ago 1930 views 1 answer
45

I need to run background tasks in Django. How do I set up Celery?

1 Answer

One accepted
18

Accepted answer

Not confirmed since it was written, 9 months, 1 week ago
# celery.py in project folder
from celery import Celery
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
app = Celery('myproject')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

# settings.py
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'

# tasks.py
from celery import shared_task

@shared_task
def send_email_async(user_id):
    # Send email logic
    pass

# Usage
send_email_async.delay(user_id)

Your answer

Say what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.

Shown beside your answer and never checked, so it cannot be a name someone already has here.

No account needed to answer. Sign in instead and it carries your name and earns reputation.