How to Configure Celery within Django

Celery is an open source asynchronous task queue/job queue based on distributed message passing[1].
Within this article we will provide the configuration steps requiring for installing celery within Django. Our tutorial



Within this example we will use Redis as the broker. Celery uses a broker to pass messages between your application and Celery worker processes[2].


The layout in this example is based on a project called projectx, and an app called appy. Below is the layout,

    |-- projectx
    |   |--
    |   |--
    |   |--
    |   |--
    |   |--
    |   |--
    `-- appy


First of all we install our packages.

yum install redis-server
pip install celery[redis]
pip install django-celery

Project Configuration

Next we configure the necessary files within our project.


To ensure that the Django app initiates the Celery app each time it is run.

from __future__ import absolute_import
from .celery import app as celery_app


We then configure Django with the necessary celery settings.

BROKER_URL = 'redis://localhost:6379/0'

INSTALLED_APPS += ("djcelery", )

INSTALLED_APPS += (“djcelery”, )


A file is created called Within this file we define our celery app.

from __future__ import absolute_import
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'projectx.settings')

from django.conf import settings   
app = Celery('projectx')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

App Configuration


Within our app we create a file called Within a simple function is created using the @shared_task decorator. This is the ‘task’ that will be sent to the task queue.

from celery import shared_task

def do_something():
    return 1 + 1

Running Worker

/opt/pythonenv/projectx/bin/python2.7 /opt/pythonenv/projectx/bin/celery --app=projectx.celery:app worker --loglevel=INFO


Now that everything is configured. We can test that everything is working. We import our and then send our task to the queue.

>>> from appy.tasks import do_something
>>> do_something.delay('celery test')
<AsyncResult: 99e35cf7-0a2d-4781-b446-2e3dd45a5c16>




Rick Donato

Want to become a Django expert?

Here is our hand-picked selection of the best courses you can find online:
The Complete Web Development Bootcamp course
Django Practical Guide course
Django Full Stack Developer Bootcamp
and our recommended certification practice exams:
AlphaPrep Practice Tests - Free Trial