Django
Celery
Periodic Tasks
Python
Web Development

Examples of Django and Celery Periodic Tasks

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Django, a high-level Python web framework, promotes rapid development and clean, pragmatic design, whereas Celery is a powerful, flexible asynchronous task queue/job queue based on distributed message passing. Together, Django and Celery can be used to handle "background" tasks such as sending emails or processing large data at periodic intervals.

Understanding Django and Celery

Before diving into periodic tasks, it’s essential to understand the basic integration of Celery with Django:

Celery Integration:

To integrate Celery into a Django project:

  1. Install Celery: Add Celery to your environment using pip:
bash
   pip install celery
  1. Configure Celery: Create a new file celery.py in your Django project’s main app and configure Celery to use a broker like RabbitMQ, Redis, etc.:
python
1   from __future__ import absolute_import, unicode_literals
2   import os
3   from celery import Celery
4
5   os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')
6
7   app = Celery('your_project')
8   app.config_from_object('django.conf:settings', namespace='CELERY')
9   app.autodiscover_tasks()
  1. Run the Celery worker: After setting your broker in settings.py, run the Celery worker:
bash
   celery -A your_project worker -l info

Setting Up Periodic Tasks

Celery uses the concept of scheduled jobs known as Beat tasks, which are executed by the Celery worker at regular intervals, defined by the scheduling parameters.

Beat Installation:

You need to install the django-celery-beat extension that stores the schedule in the Django database, and provides models and admin interface to manage periodic tasks easily:

bash
pip install django-celery-beat

Configure Periodic Tasks:

  1. Modify Django’s settings.py to include django_celery_beat:
python
1   INSTALLED_APPS = [
2      ...,
3      'django_celery_beat',
4   ]
  1. Define Tasks: In any app within your Django project, you can define tasks in a tasks.py file:
python
1   from celery import shared_task
2
3   @shared_task
4   def send_notification():
5       # Your task implementation
6       print("Notification sent!")
  1. Schedule Tasks: You can schedule the above task directly from Django Admin or by creating a periodic task programmatically:
python
1   from django_celery_beat.models import PeriodicTask, CrontabSchedule
2
3   schedule, created = CrontabSchedule.objects.get_or_create(
4       minute='0',
5       hour='0',
6       day_of_week='*',
7   )
8
9   task = PeriodicTask.objects.create(
10       crontab=schedule,
11       name='Send Notification Every Midnight',
12       task='your_app.tasks.send_notification'
13   )

Benefits and Caveats:

Using Django and Celery for periodic tasks offers scalability and efficient handling of background tasks but be aware of time zone issues and ensure that tasks are idempotent, particularly important in distributed systems.

Summary Table:

FeatureDescriptionRequired PackagesDjango Configuration
Task DefinitionDefine functions decorated with @shared_taskCelery
Task SchedulingSchedule tasks using Crontab or intervalsdjango-celery-beatAdd to INSTALLED_APPS
ExecutionTasks are executed by Celery workersRun Celery worker
ManagementManage via Django Admin or programmaticallydjango-celery-beatUse PeriodicTask models

Advanced Tips:

  • Ensure you have a robust retry logic and error handling for tasks since failures are inevitable in production environments.
  • Monitor your tasks periodically using tools like Flower, which provides a web interface to monitor the Celery tasks and workers.

In conclusion, integrating Celery with Django for scheduling and managing periodic tasks provides a powerful toolset for developers to handle background processes efficiently. These tools empower applications to perform complex operations asynchronously which optimizes resource usage and enhances application responsiveness.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.