All I want to do is. . .

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

OS:
Tags:

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 https://raw.github.com/Homebrew/homebrew/go/install)"
    
    # 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 http://download.redis.io/redis-stable.tar.gz
$ tar xvzf redis-stable.tar.gz
$ cd redis-stable
$ make

Then, add the following to your requirements file:

# requirements.txt
celery-with-redis
django-celery

Install the new requirements:

$ pip install -r requirements.txt

Add the following to setup.py:

# settings.py
import djcelery
djcelery.setup_loader()
...

# Add djcelery to installed apps
INSTALLED_APPS = [
    ...
    'djcelery',
    ...
]

# 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 manage.py migrate djcelery
# otherwise:
$ python manage.py 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 manage.py 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 manage.py celerycam

You are now ready to run Celery tasks.

Tasks can be defined like so:

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

@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:

Raw: django-celery-redis.md


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

Licensed under the CC-SA.