Sunday, May 24, 2020

HOW TO USE CELERY AND RABBITMQ WITH DJANGO

Introduction : 

     Celery is an asynchronous task queue based on distributed message passing. Task queues are  used as a strategy to distribute the workload between threads/machines .
    In this blog I am going to show you how to implement CELERY and RABBITMQ with Django .

Used Packages for Implementation :

Install Rabbitmq Server on Your system .
Install Django==1.11.29 and celery==4.4.2


Let's Start Implemention :  

Create a file celery.py in your project root folder where settings file is located .

Add lines to your celery.py file given below .


from __future__ import absolute_import
import os
from celery import Celery

# set the default Django settings module for the 'celery' program.
# replace blog_celery with your project name
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'blog_celery.settings')

app = Celery('blog_celery')

app.config_from_object('django.conf:settings', namespace='CELERY')

# CELERY Message Broker(Rabbimq) .
CELERY_BROKER_URL = "amqp://"

CELERY_TIMEZONE = "Asia/Kolkata"

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

Add Line Given below in __init__.py of project root path .
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ['celery_app']

So setup part is over . Now let's See how to Use celery in your function to implement asynchronous task .


Create utils.py in any django app and add code given below for testing .

from blog_celery.celery import app
import time

@app.task
def run():
time.sleep(5)
print("I am running via celery")


Now Call the created task in view.py . Example code given below .

from django.shortcuts import render
from django.http import HttpResponse
# import task created to run via celery
from .utils import run

def task(request):
# to run celery task task_name.delay() is used
run.delay()
return HttpResponse("Celery Task are running")


Now run the celery worker to run your  celery task- 

celery -A blog_celery worker -l debug
Note: Replace blog_celery with your project name .

If You have any suggestion or doubt then comment below .
Thank You