← Back to Chapters

Celery for Background Tasks

⚙️ Celery for Background Tasks

? Quick Overview

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.

? Key Concepts

  • Asynchronous Execution: Tasks run independently from web requests.
  • Message Broker: Redis or RabbitMQ transports tasks to workers.
  • Workers: Background processes that execute tasks.

? Syntax & Theory

Celery works by sending messages (tasks) to a broker. Worker processes listen to the broker, pick up tasks, and execute them asynchronously.

? Code Examples

? View Code Example
# Install required packages for Celery and Redis
pip install celery redis
? View Code Example
# 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()
? View Code Example
# 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'
? View Code Example
# 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!"
? View Code Example
# 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.'})

? Live Output / Explanation

When the view is accessed, the response is returned immediately while the email task is processed by a Celery worker in the background.

? Interactive Flow Diagram

Django App Redis Broker Celery Worker

? Live Simulation

Django
 
Redis
 
Worker
System Ready

? Use Cases

  • Sending emails asynchronously
  • File processing and uploads
  • Scheduled background jobs
  • Data analysis and reports

✅ Tips & Best Practices

  • Always keep the Celery worker running.
  • Use retries for unstable external services.
  • Separate long-running tasks from user requests.

? Try It Yourself

  • Send background emails using Celery.
  • Create periodic tasks with Celery Beat.
  • Monitor task states using result backend.