How to set up Celery in a Django project?

How to set up Celery in a Django project?

Celery, Celery, Celery, oh my darling Celery!

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 RabbitMQ server, on Ubuntu the package name is rabbitmq-server :

      sudo apt-get install rabbitmq-server
    

    Once you are done with the installation, you can start the service:

      sudo systemctl enable rabbitmq
      sudo systemctl start rabbitmq
    
  • On Windows based OS:

    Please follow this link for setting up the RabbitMQ server 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.py just in the same folder where settings.py resides 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__.py file, just in the same folder where settings.py resides 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.

Did you find this article valuable?

Support Dev.Junction by becoming a sponsor. Any amount is appreciated!