How to set up Celery in a Django project?
Celery, Celery, Celery, oh my darling Celery!

Experienced Full Stack Software Engineer with 4+ Years of experience, proficient in Django and React, with expertise in Python, JavaScript and Typescript, also an author of Dev.Junction Blogs & YouTube Channel, expert in Python/Django, Rest Framework, ReactJS/NextJS and TypeScript, currently based in Pune, India.
Introduction:
This is the second part of this blog series where we are learning to use Celery in a Django project, check out the full series here.
In this blog, we are going to learn how to set up Celery & our Task broker in a Django project.
We’ll be using RabbitMQ as our task broker.
Installation:
Installing & Running RabbitMQ:
As I said in the previous blog, RabbitMQ is a task broker, which means it stores the tasks came from Django and passes it to Celery for further execution.
So to use Celery in Django, we have to run a RabbitMQ server to connect it with Celery instance.
On Linux or Mac or any Unix based system, you have to install a package for
RabbitMQserver, on Ubuntu the package name israbbitmq-server:sudo apt-get install rabbitmq-serverOnce you are done with the installation, you can start the service:
sudo systemctl enable rabbitmq sudo systemctl start rabbitmqOn Windows based OS:
Please follow this link for setting up the
RabbitMQserver in your system. https://www.rabbitmq.com/install-windows.html
Installing Celery:
Now just activate your Django project’s virtual environment, and install the Celery:
pip install celery
Celery in Django:
So now that we have installed all the required packages, it's time to integrate Celery with our Django project. I am assuming that you have already up and running Django project (without Celery).
- Now the first step is to create a new file named
celery.pyjust in the same folder wheresettings.pyresides and paste this code inside:
Note: Few things I am assuming here is that, the Django project name is myproject you should use your project name instead.
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
# You can use anything as your Celery instance name,
# I am using "myproject_celery" as my celery instance name
app = Celery("myproject_celery")
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object("django.conf:settings", namespace="CELERY")
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
- Now the next step is to connect Celery with Django, to do that simply open the
myproject/__init__.pyfile, just in the same folder wheresettings.pyresides and paste this code inside:
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ("celery_app",)
Running Celery:
If you have followed all the steps above, you are ready to start your Celery instance, and to do that simply run this command:
celery -A myproject.celery worker -l info
Note: myproject is the name of our project, which we have used above in celery.py file.
This is it for this blog, in our next blog, we will create our first asynchronous task and see how we can run it using our current setup.
For more such crispy blogs daily, follow Dev.Junction, subscribe to our newsletter and get notified.
Social Links
- LinkedIn: https://www.linkedin.com/in/mnamegaurav/
- YouTube: https://www.youtube.com/c/devjunction
- Website: https://gaurav.devjunction.in/
- GitHub: https://github.com/mnamegaurav
- Instagram: https://www.instagram.com/mnamegaurav/
- Twitter: https://twitter.com/mnamegaurav






