Django-celery

Celery integration with Django

Celery is nothing but, It is a task queue that helps, manage the asynchronous tasks in a distributed environment. It works by sending messages between Django applications and worker processes through a message broker such as RabbitMQ or Redis. One of the main advantages of celery is to offload some asynchronous tasks and run independently at regular period of intervals. (programspeaker)

Now we can create a celery application here;

Step 1: First we need to create a new Django project and add an app named MyCelery

django-admin startproject Celery
cd Celery
django-admin startapp CeleryApp

Step 2: Then install Celery and the required dependencies. We can do this by running the following command.

pip install django-celery

Also, install the Redis package and its server

pip install Redis
sudo apt-get install redis-server

Step 3: Add MyCelery and celery into the INSTALLED_APPS List in Django settings.

'CeleryApp', 'celery'

Step 4: Add the following code to the settings.py file

# set the celery broker url
CELERY_BROKER_URL = 'redis://localhost:6379/0'
  
# set the celery result backend
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'

Here CELERY_BROKER_URL is the URL of the message broker used to send and receive messages. And CELERY_RESULT_BACKEND is the backend to store task results.

Step 5: In the __init__.py file add the following code section. Here we import the Celery and create an instance of the celery class.

from .celery import app as celery_app
__all__ = ['celery_app']

Step 6: Create a celery.py in the Django project root.

import os
from celery import Celery

os.environ.setdefault
	('DJANGO_SETTINGS_MODULE', 'Celery.settings')
app = Celery('Celery')
app.config_from_object('django.conf:settings',
					namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

Step 7: Create a task file named task.py in the app directory and add the following code section

from celery import shared_task
  
@shared_task
def printLoop():
      for i in range(10):
         print(i)
      return "Completed"

Step 7: Next we call the celery task by adding the following code

from CeleryApp.task import printLoop
result = printLoop.apply_async(countdown=5)

Step 7: Once we defined our task, we will need to start the Celery worker process.

python manage.py runserver
celery -A Celery worker -l info
redis-server

Run the following commands in different terminals or create a service file for each.

Share this:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top