Celery is a powerful, asynchronous task queue/job queue system that is used to handle background tasks in Python applications. In Django, Celery is commonly used to perform long-running tasks such as sending emails, processing files, or performing data analysis, outside of the request/response cycle.
Celery works by sending messages (tasks) to a broker. Worker processes listen to the broker, pick up tasks, and execute them asynchronously.
# Install required packages for Celery and Redis
pip install celery redis
# Celery configuration file
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
app = Celery('myproject')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
# Redis configuration inside settings.py
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_TIMEZONE = 'UTC'
# Defining an asynchronous task
from celery import shared_task
@shared_task
def send_email_task(email):
print(f"Sending email to {email}")
return "Email sent!"
# Calling Celery task from Django view
from django.http import JsonResponse
from .tasks import send_email_task
def send_email_view(request):
send_email_task.delay('example@example.com')
return JsonResponse({'message': 'Email is being sent in the background.'})
When the view is accessed, the response is returned immediately while the email task is processed by a Celery worker in the background.