Setting Up CELERY in Django
Tags: django, python, webdev, celery
Published: 2022-06-03 09:16:57

## 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
```

**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' 
```

**Step 3:** Install redis using below link if you are using windows 
[Download for windows](https://github.com/tporadowski/redis/releases)
[Download for Linux](https://redis.io/docs/getting-started/installation/install-redis-on-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
```

**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))
```

**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
```
**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"
```
**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',)
```

**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")
```


> If you are using django inbuilt **user modal** install the other package for presenting the status in admin panel
```
pip install django-celery-results
```

**Step 10:** Then Create Super using command
```
python manage.py createsuperuser
```

**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
```

> **Your Task status will look like below**

![DjangoAdmin](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ux99hiz7rx4klvjv89k6.png)

[Ecourge Innovations](https://ecourge.com)