DEV Community

Cover image for Setting Up CELERY in Django
Kaushal Patel
Kaushal Patel

Posted on • Updated on

Setting Up CELERY in Django

What is CELERY

Celery is an open source asynchronous task queue or job queue which is based on distributed message passing. While it supports scheduling, its focus is on operations in real time.

Celery 5.0.x supports Django 1.11 LTS or newer versions. Please use Celery 4.4.x for versions older than Django 1.11.

Step 1: First You need to install celery using pip package manager

pip install celery
Enter fullscreen mode Exit fullscreen mode

Step 2: Change Settings.py file and add below configuration details at end of file

CELERY_BROKER_URL = 'redis://127.0.0.1:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Kolkata' # Configure Time zone based on your webserver
CELERY_RESULT_BACKEND = 'django-db' 
Enter fullscreen mode Exit fullscreen mode

Step 3: Install redis using below link if you are using windows
Download for windows
Download for Linux

Step 4: Create celery.py file in project folder
eg:

- celery_with_django/
  - manage.py
  - celery_with_django/
    - __init__.py
    - settings.py
    - urls.py
    - celery.py
Enter fullscreen mode Exit fullscreen mode

Step 5: Add following code for simple testing celery

from __future__ import absolute_import, unicode_literals
import os

from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'PROJECT_NAME.settings')

app = Celery('celery_with_django')
app.conf.enable_utc = False

app.conf.update(timezone = 'Asia/Kolkata') # Configure Time zone based on your webserver

app.config_from_object(settings, namespace='CELERY')


app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))
Enter fullscreen mode Exit fullscreen mode

Step 6: Then create App using following code and add tasks.py file

- celery_with_django/
  - manage.py
  - celery_with_django/
    - __init__.py
    - settings.py
    - urls.py
    - celery.py
  - main_app
    - __init__.py
    - apps.py
    - admin.py
    - tests.py
    - tasks.py
    - models.py
    - views.py
Enter fullscreen mode Exit fullscreen mode

Step 7: Add Below code Into the tasks.py file it it will create a shared task which will print 0 to 10

from celery import shared_task

@shared_task(bind=True)
def test_func(self):
    #operations
    for i in range(10):
        print(i)
    return "Done"
Enter fullscreen mode Exit fullscreen mode

Step 8: Then change the init.py file in main_app

# 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',)
Enter fullscreen mode Exit fullscreen mode

Step 9: Then call the test_func() from the views.py file

from django.http.response import HttpResponse
from django.shortcuts import render
from .tasks import test_func

# Create your views here.
def test(request):
    test_func.delay()
    return HttpResponse("Done")
Enter fullscreen mode Exit fullscreen mode

If you are using django inbuilt user modal install the other package for presenting the status in admin panel

pip install django-celery-results
Enter fullscreen mode Exit fullscreen mode

Step 10: Then Create Super using command

python manage.py createsuperuser
Enter fullscreen mode Exit fullscreen mode

Step 11: Then Start Server and celery worker

# Starting Django Sever
python manage.py runserver

# Staring Celery Worker
celery -A celery_with_django.celery worker --pool=solo -l INFO
Enter fullscreen mode Exit fullscreen mode

Your Task status will look like below

DjangoAdmin

Ecourge Innovations

Top comments (0)