Simple Django, Celery, RabbitMQ start

(Originally posted July 12, 2013)

Considering email is a big part in reminding people to do things, we need a way to distribute the sending of emails somewhat asynchronously. To do this, the consensus way in Django is to use Celery with RabbitMQ. After sifting through a bunch of different posts on the subject, I decided to write a little about how I got the initial setup going. I’m using a mac with homebrew installed.

The first thing to do is install and get RabbitMQ going.

brew install rabbitmq

With rabbitMQ installed, we now want to get the rabbit server running.

sudo rabbitmq-server -detached

will start the server in the background.

To make sure it’s running, run the command,

sudo rabbitmqctl status

You should see a long nested bracket output with information regarding the status of the server. If you see that, you can be sure it is running. To stop the server, never just kill the pid, rather run

sudo rabbitmqctl stop

and this will stop the server. Note that it’s important to run all these commands with sudo. Otherwise, you might see a message of an erlang dump meaning there was an error.


$ brew install celery

will install celery! We then want to use pip to install the django connector

$ pip install django-cellery

Add ‘djcellery’ to installed apps and run (since you’re hopefully using South),

$ python migrate djcellery

We also need to add a few other things to get the django connection finished. In, add the following lines

import djcelery
CELERY_IMPORTS = ('<project_name>.tasks',)

where <project_name> is the name of your projects. The CELERY_IMPORTS line can actually be the path to any file that has tasks defined, whether it is a file or not.

Conventionally, it tasks for cellery are in a file called which can reside anywhere in the project tree. For this simple starter task, the task will run every minute, and print a simple string to the command line. This takes no extra configuration and is simple to see if it is all working.

from celery.task.schedules import crontab
from celery.decorators import periodic_task
def project_tasks():
    print 'hello world!'

Wherever you put this, just make sure that has the correct path so that it can find the definition.

Running celery

The last thing to do is to run celery and see the output!

python celeryd -v 2 -B -s celery -E -l INFO

After letting it run for a minute, you should see, along with some other celery information, hello world! being printed!

Obviously this isn’t that impressive, but now you know you’re able to run python code periodically. Now go do something more impressive than hello world.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s