How to implement a task queue in Django using Celery



Image not found!!

Implementing a task queue in Django using Celery involves several steps. Celery is a distributed task queue system that can be used to offload time-consuming tasks from your web application. Here's a step-by-step guide on how to set up and use Celery with Django:

Step 1: Install Celery

bash
pip install celery

Step 2: Configure Celery in Django

Create a file named celery.py in your Django project directory (beside your settings.py file):

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()

Replace 'your_project' with the actual name of your Django project.

Step 3: Configure Celery in Django settings

In your settings.py, add the following configuration for Celery:

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'

Make sure to replace the broker URL (CELERY_BROKER_URL) and result backend (CELERY_RESULT_BACKEND) with your own setup. In this example, Redis is used as the message broker.

Step 4: Create a Celery Task

Create a file, for example, tasks.py in one of your Django apps:

python
# tasks.py from celery import shared_task @shared_task def add(x, y): return x + y

Step 5: Use Celery Task in Django

You can now use the Celery task in your Django views, models, or any other part of your application. For example, in a view:

python
# views.py from django.shortcuts import render from .tasks import add def my_view(request): result = add.delay(4, 4) return render(request, 'my_template.html', {'task_id': result.id})

Step 6: Run Celery

Open a terminal and run the Celery worker:

bash
celery -A your_project worker -l info

Open another terminal and run the Celery beat (optional, for periodic tasks):

bash
celery -A your_project beat -l info

Make sure to replace 'your_project' with the actual name of your Django project.

That's it! You've now set up Celery in your Django project with a basic task. Adjust the configurations and tasks according to your project's requirements.