All I want to do is. . .

. . .use Celery in Django with a Redis backend.


Description: Celery is a distributed task queue for Python that allows you to run computationally expensive code asynchronously. Here's how to integrate Celery in a Django project, using Redis for the backend service.
Contributors: sloria
Updated: 07/13/13

Do these first:

  • Install Homebrew
    # In your Mac Terminal, run:
    $ ruby -e "$(curl -fsSL"
    # In your ~/.bashrc, ~/.bash_profile, or ~/.zshrc (for zsh users)
    # ...
    export PATH=/usr/local/bin:$PATH
    $ brew doctor
     [full instructions]

First, install redis:

# On MacOSX with homebrew
$ brew update
$ brew install redis

# On Linux
$ wget
$ tar xvzf redis-stable.tar.gz
$ cd redis-stable
$ make

Then, add the following to your requirements file:

# requirements.txt

Install the new requirements:

$ pip install -r requirements.txt

Add the following to

import djcelery

# Add djcelery to installed apps

# Celery settings
BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'

Create the database datatables:

# If you're using South:
$ python migrate djcelery
# otherwise:
$ python syncdb

Now to start your redis server and a celery worker.

# Start the redis server
$ redis-server
# In a new terminal window/tab, start a celery worker
$ python celeryd worker -E
# In ANOTHER terminal window/tab, enable the Django Celery monitor
# so that you can manage your tasks from your admin page
$ python celerycam

You are now ready to run Celery tasks.

Tasks can be defined like so:

# (in one of your apps)
from celery import task

def add(x, y):
    return x + y
# In some other file (such as in views or models)
from myapp.tasks import add

add.delay(2, 2)  # Asynchronous addition--Huzzah!

See also:


If you have suggestions, corrections, or content to contribute, fork us at our Github repo or open an issue.

Licensed under the CC-SA.