Use celery and RabbitMQ with Django Rest API.

Rijin Swaminathan
2 min readAug 1, 2021

--

Celery is a simple, flexible, and reliable distributed system to process vast amounts of messages while providing operations with the tools required to maintain such a system. It’s a task queue with a focus on real-time processing, while also supporting task scheduling.

RabbitMQ is a message-queuing software also known as a message broker or queue manager. Simply said; it is software where queues are defined, to which applications connect to transfer a message or messages. A message broker acts as a middleman for various services (e.g. a web application, as in this example). They can be used to reduce loads and delivery times of web application servers by delegating tasks that would normally take up a lot of time or resources to a third party that has no other job.

Preparation

Here I implement this with Django so I assume you have the basic knowledge that how to set up a Django project. So set up the project with a basic register user API and we verify the user with an OTP verification in that API we implement celery.

Setting up RabbitMQ in your machine.

Install RabbitMQ Server

sudo apt-get install rabbitmq-server

Enable RabbitMQ Server

sudo systemctl enable rabbitmq-server

Start RabbitMQ Server

sudo systemctl start rabbitmq-server

Status of RabbitMQ Server

sudo systemctl status rabbitmq-server

Stop RabbitMQ Server

sudo rabbitmqctl stop

These are the basic commands to set up the RabbitMQ in the System. Now we want to install the celery with our Django application for that like any package we can install using the pip command.

pip install celery

Now get into the code first, we need to create a file with the name celery.py and add the below code in the file.

then the recommended way is to create a new proj/proj/celery.py module that defines the Celery instance:

proj mentioned in the bellow code is your project name so replace it with your project name.

reference: https://docs.celeryproject.org/en/stable/django/first-steps-with-django.html#using-celery-with-django

Now set up the task with the celery to execute the task asynchronously.

For that, we create a file named tasks.py in this project we use celery to send OTP asynchronously. So the code here is,

So when we invoke the method it will execute this method so it will execute what we need asynchronously.

To perform the method the method will be,

delay keyword is very important don’t forgot to put it.

So after setup these things in your project you are ready to run the code block asynchronously.

For reference, you can check the code.

https://github.com/RijinSwaminathan/django_email_celery

To run the project with the celery instance use this command.

celery -A project name worker -l info

Kindly put your comments that will be helpful for my future write ups. Thank you

--

--

Rijin Swaminathan

Software Engineer, Enthusiast on knowing the technologies.