Background Tasks with Celery & Redis
Celery enables you to delegate slow execution tasks (sending emails, image processing, or data imports) to background worker queues, keeping the main request-response cycle fast.
1 Creating Celery Tasks
Python — tasks.py
from celery import shared_task
from django.core.mail import send_mail
@shared_task
def send_welcome_email(email):
send_mail(
"Welcome",
"Thank you for registering!",
"noreply@example.com",
[email],
fail_silently=False,
)
2 Code Challenge
Challenge: Configure Celery Beat to execute a recurring database cleaning script executing every midnight cleanly.