Implementing background tasks in Django using Celery involves several steps. Celery is a distributed task queue system for Python that allows you to execute tasks asynchronously. Here's a step-by-step guide on how to set up background tasks using Celery in a Django project:
Install Celery using pip:
bashpip install celery
Create a file named celery.py
in your Django project directory (next to settings.py
):
python# celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')
# create a Celery instance and configure it using the settings from Django
app = Celery('your_project')
# Load task modules from all registered Django app configs.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Auto-discover tasks in all installed apps
app.autodiscover_tasks()
Make sure to replace 'your_project' with the actual name of your Django project.
Add the following lines to your Django project's settings.py
file:
python# settings.py
# Celery configuration
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'UTC'
In this example, Redis is used as the message broker. You can replace the CELERY_BROKER_URL
and CELERY_RESULT_BACKEND
with the connection strings of other supported message brokers (RabbitMQ, etc.).
In your Django app, create a file named tasks.py
and define a Celery task:
python# tasks.py
from celery import shared_task
from time import sleep
@shared_task
def my_background_task(arg1, arg2):
# Task logic
sleep(5)
return arg1 + arg2
In your views or models, you can now call the Celery task:
python# views.py
from django.shortcuts import render
from .tasks import my_background_task
def my_view(request):
result = my_background_task.delay(3, 4)
return render(request, 'my_template.html', {'result_id': result.id})
Run the Celery worker to process tasks:
bashcelery -A your_project worker --loglevel=info
Make sure to replace 'your_project' with your actual Django project name.
That's it! Now, when you call my_background_task.delay(3, 4)
, it will be executed asynchronously by Celery in the background. Adjust the configuration and tasks according to your project's requirements.