Django + celery + rabbitmq – Zaigo Infotech Software Solutions

Let’s craft brilliance together!

Request a free consultation and get a no-obligation quote for your project within one working day.

Company-Logo

Error: Contact form not found.

Django + celery + rabbitmq

Django

Python

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

 

 

Can't find what you are looking for?

Post your query now, and we will get in touch with you soon!

    Want to start a project?

    Our team is ready to implement your ideas. Contact us now to discuss your roadmap!

    GET IN TOUCH

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    INDIA

    9thfloor, (9A & 9B) Sapna Trade Centre, 135,
    Old 109, Poonamallee High Rd, Egmore,
    Chennai, Tamil Nadu 600084

    +91 9884783216

    marketing@zaigoinfotech.com

    USA

    170 Post Rd #211, Fairfield,
    CT 06824,
    USA

    +1 904-672-8617

    sales@zaigoinfotech.com