Exploring Running Background Tasks with Django Celery Using Redis

In today’s fast-paced world, users expect applications to perform tasks quickly, even when those tasks require large resources and energy. To achieve this, developers can use Celery, a powerful library for asynchronous processing. Celery is a distributed task queue. Once you integrate Celery into your app, you can send time-consuming tasks to Celery’s task queue. Django Celery is a powerful tool for handling these tasks in the background.

Understanding the Need for Background Tasks

Users often initiate tasks that can be time-consuming—for example, sending email notifications, processing large files, or performing complex computations. In a synchronous setup, these tasks would block the main application thread, resulting in slow response times for users. This is where Celery comes in.

Example: In a project, I was required to upload multiple videos convert them into a different format, and then upload them to the AWS bucket, which is a time-consuming task. So I use Redis-celery to process the video convert them to different formats and then upload them to the bucket, which does not block the main thread for performing other tasks.

Introduction to Celery and Redis

Celery is a powerful asynchronous task queue that allows you to offload time-consuming tasks to be processed in the background.

Redis serves as the message broker for Celery, facilitating communication between the application and the Celery worker processes.

Setting Up Django Celery with Redis

🔸 Installing Celery and Redis

To get started, we need to install Celery and Redis. For installing celery:

Syntax: pip install celery

For installing Redis:

Syntax: pip install celery (redis)

Integrating Celery with Django involves configuring settings, such as specifying the broker URL and defining the task serializer.

🔸 Configuring Redis as the Message Broker

Redis provides the necessary infrastructure for Celery to distribute tasks among worker processes.

Related read: Spring Boot With Redis Cache Using Annotation

Boost Your App's Performance with Django Celery & Redis! Hire Our Developers

Creating a Celery Task

🔸 Defining a Celery Task in Django

The process of creating a Celery task in a Django application. This includes writing the task function and applying the appropriate Celery decorators.

Steps ➡️

Create a celery.py file in your Django project directory:

# django_celery/celery.py

import os
from celery import Celery

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_celery.settings")
app = Celery("django_celery")
app.config_from_object("django.conf:settings", namespace="CELERY")
app.autodiscover_tasks()

Configure your Django settings to use Celery as the task queue:

# django_celery/settings.py

# Celery settings
CELERY_BROKER_URL = "redis://localhost:6379"
CELERY_RESULT_BACKEND = "redis://localhost:6379"

To make sure that your Celery app is loaded when you start Django, you should add it to project __init__.py :

# django_celery/__init__.py

from .celery import app as celery_app
__all__ = ("celery_app",)

Create a task.py in your application:

# django_app/task.py
from celery import shared_task

@shared_task
def my_background_task():
pass

Call this task from your views or models when you want it executed asynchronously.

Finally, you can now also start Celery correctly without running into an error message:

python -m celery -A django_celery worker

Monitoring Celery Tasks

🔸 Flower: A Real-Time Monitoring Tool

Flower is a real-time web-based monitoring tool for Celery. We can use Flower to gain insights into Celery task execution.

coma

Conclusion

The advantages of using Django Celery with Redis for background task processing. The need for background tasks arises when dealing with time-consuming operations, such as processing large files or performing complex computations, that would otherwise slow the main application thread’s responsiveness.

By using Celery as a distributed task queue and Redis as the message broker, developers can seamlessly offload these tasks to the background, ensuring a smoother user experience.

Keep Reading

Keep Reading

Launch Faster with Low Cost: Master GTM with Pre-built Solutions in Our Webinar!

Register Today!
  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?