JyothisApril 25, 2025
Asynchronous task queues are essential when dealing with time-consuming operations like sending emails, generating reports, or processing files. Celery is the go-to task queue for Python developers. In this guide, we'll walk through how to set up Celery with Django on both Ubuntu and Windows environments.
Celery is an asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operation but supports scheduling as well. Celery requires a message broker to handle task messages—Redis and RabbitMQ are common choices.
Install Celery and Redis packages in your virtual environment:
After installation, create a file called celery.py in your project root directory, and a tasks.py file in your app directory for defining tasks.
Your project structure will look like this:
celery.py (in the same directory as settings.py):
__init__.py (same directory as settings.py):
settings.py
tasks.py (inside your app):
Although Linux is the preferred OS for Celery, you can still run it on Windows for development.
Step 1: Install Required Packages
Step 2: Install and Run Redis
Option 1: Memurai (recommended)
Option 2: Redis for Windows (community-maintained)
Test Redis with:
(Should return PONG)
Step 3: Run Celery on Windows
Celery uses the prefork pool by default, which does not work on Windows. Use the solo pool instead.
In celery.py, add:
In CMD or PowerShell:
In PowerShell Core or Git Bash:
Test the Celery Task
Celery is a powerful tool for handling asynchronous tasks in Django, making your application more efficient and user-friendly. By following this guide, you can set up Celery with Redis on both Ubuntu and Windows, create and test tasks, and even schedule periodic jobs. Whether you're developing locally or preparing for production, Celery's flexibility ensures your Django app can handle heavy workloads with ease.
For further reading, check the Celery documentation or Django Celery Beat.
Happy task queuing!
0