You are currently viewing Django and Celery: Task Scheduling and Background Jobs

Django and Celery: Task Scheduling and Background Jobs

Django and Celery are commonly used together to handle task scheduling and background jobs in Django applications. Celery is a distributed task queue system that allows you to execute tasks asynchronously in the background. Here’s an overview of how to integrate Celery with Django for task scheduling and background jobs:

  1. Install Celery:
  • Install Celery using pip: pip install celery
  1. Configure Celery in Django:
  • Create a new file called celery.py in your Django project’s root directory.
  • In celery.py, configure Celery to use your Django settings and set up the broker (e.g., RabbitMQ, Redis, or others) for task messaging.
  • Here’s an example celery.py file: from __future__ import absolute_import, unicode_literals import os from celery import Celery # Set the default Django settings module os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings') app = Celery('your_project') # Load task modules from all registered Django app configs app.autodiscover_tasks()
  1. Define Celery Tasks:
  • Create a tasks.py file in your Django app’s directory or any other location.
  • Define your Celery tasks as regular Python functions and decorate them with @app.task from Celery.
  • Here’s an example of a task that sends an email asynchronously: from your_project.celery import app from django.core.mail import send_mail @app.task def send_email_task(subject, message, from_email, recipient_list): send_mail(subject, message, from_email, recipient_list)
  1. Start Celery Worker:
  • Open a terminal and navigate to your Django project’s root directory.
  • Start the Celery worker using the following command:
    celery -A your_project worker --loglevel=info
  1. Triggering Celery Tasks:
  • To trigger a Celery task, simply import the task and call it like any other Python function.
  • Here’s an example of how to trigger the send_email_task: from your_app.tasks import send_email_task send_email_task.delay('Subject', 'Message', 'from@example.com', ['to@example.com'])
  • The delay() method is used to enqueue the task for execution asynchronously.
  1. Task Scheduling:
  • Celery also supports task scheduling using the Celery Beat scheduler.
  • Configure periodic tasks in your Django project’s settings.py file using the CELERY_BEAT_SCHEDULE setting.
  • Here’s an example of scheduling a task to run every 5 minutes: # settings.py CELERY_BEAT_SCHEDULE = { 'task-name': { 'task': 'your_app.tasks.task_function', 'schedule': timedelta(minutes=5), }, }
  • The task_function is the function that you want to schedule, and timedelta(minutes=5) specifies the interval.

By integrating Celery with Django, you can offload time-consuming or resource-intensive tasks to run in the background, improving the responsiveness and scalability of your application.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.