Django Blog

The objective of this blog is to dive deep into celery, its application and working, and finally integrating it into Django project. The first part of this article consists of theoretical understanding of celery while the second part shows the implementation.

Maulik Patel
March 12, 2024
Last Updated April 03, 2024
SWAN
None

Django Celery

The objective of this blog is to dive deep into celery, its application and working, and finally integrating it into Django project. The first part of this article consists of theoretical understanding of celery while the second part shows the implementation.

What is celery and its application?

Celery is a Python-based tool that allows you to execute tasks asynchronously in the background, without blocking the main program flow. It is used to handle time-consuming or resource-intensive tasks by executing them in the background thus providing a better user experience.

We can use celery for various purposes some of which include:

  • Sending emails to a large chunk of users.
  • Running the machine learning task.
  • Generating reports for a large number of users.

Diving Deep into celery

To understand celery architecture better it can be broadly divided into five components as producertask queueworker, and result backend. Let's understand each of them in detail.

Implementation

For implementation purposes, I have created basic a task that sends the email using celery. I will be assuming that Django and redis already present in your system. I will be using redis as the celery broker and Django-db as the backend.

Installing modules

To run celery following modules need to be installed:

  • celery==5.2.7
  • django-celery-results==2.5.0

Django-celery-results provide result backends for Celery tasks. It allows us to store the results of Celery tasks in your Django database, so we can access and manipulate them using Django’s ORM.

Setting files

Following changes are needed in the settings.py file

CELERY_BROKER_URL='redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT=['application/json']
CELERY_RESULT_SERIALIZER='json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE='Asia/Kolkata'
CELERY_RESULT_BACKEND='django-db'

The above code will set Redis as the broker and we are setting the Django backend to store celery results.

Fast Track Software Project Success Now

ACCESS TO ON DEMAND PYTHON EXPERTS, RESOLVING COMPLEXITIES ON TIME

SWAN
Jack Davis
Software Development Expert [email protected]

Share blog on