Step 1: Install Required Packages
pip install celery
Step 2: Install RabbitMQ Server
sudo apt-get update
sudo apt-get install rabbitmq-server
Step 3: Start RabbitMQ Server
sudo systemctl start rabbitmq-server
Step 4: Check RabbitMQ Status
systemctl status rabbitmq-server
Step 5: Configure Celery in Django
Create a file named celery.py inside your project folder project_name/
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# Set default Django settings module
os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘celeryproj.settings’)
# Create Celery app instance
app = Celery(‘celeryproj’)
# Load custom config from Django settings using ‘CELERY_’ prefix
app.config_from_object(‘django.conf:settings’, namespace=’CELERY’)
# Autodiscover tasks from all installed apps
app.autodiscover_tasks()
Step 6: Connect Celery with Django App
Edit your project_name/__init__.py to include:
from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared tasks are discovered.
from .celery import app as celery_app
__all__ = (‘celery_app’,)
Step 7: Create Asynchronous Tasks
from __future__ import absolute_import, unicode_literals
from celery import shared_task
from django.core.mail import send_mail
import time
import requests
import json
@shared_task
def function_name:
#logics
Step 8: Configure Celery Settings in Django
# Celery settings
CELERY_BROKER_URL = ‘amqp://localhost’ # RabbitMQ default
CELERY_ACCEPT_CONTENT = [‘json’]
CELERY_TASK_SERIALIZER = ‘json’
# Optional for storing task results (install django-celery-results)
INSTALLED_APPS += [‘django_celery_results’]
CELERY_RESULT_BACKEND = ‘django-db’
# Run migrations if you use django_celery_results
# python manage.py migrate
Step 9: Run Celery Worker
celery -A celeryproj worker -l info
Testing the Setup
sudo systemctl start rabbitmq-serve
Optional Tools
- Flower – Monitor Celery tasks:
pip install flower
celery -A celeryproj flower
Access via: http://localhost:5555
