Basic django, celery and rabbitmq example

Rabbitmq:
On OS X, this is quite easy to install. I used homebrew (brew install rabbitmq). I had problems with it assigning symlinks, so I had to fix up some permissions.
django-celery:
This is also to install. On OS X, you simply run “easy_install django-celery”. You can also use pip.
Important django parts:
settings.py
Add “djcelery” to installed apps, and add:
import djcelery
djcelery.setup_uploader()
at the top. followed by:
BROKER_URL = “amqp://guest:guest@localhost:5672//”
This line that celery uses to connect to the specific messaging broker (in this case rabbitmq). It uses the guest user and password. If you want to use your own user/password combo, you should create that with rabbitmq.
tasks.py (some tasks)
and then in views.py, you can do something basic like this:
It adds three tasks that sleeps for 10 seconds each. All will finish at the same time.
I put the example on github.
To run it all:
1) Clone from github
2) Run rabbitmq: “rabbitmq-server” (just that. Type it in terminal)
3) Syncdb: “python manage.py syncdb”
4) Run django-celery: “python manage.py celeryd –log-level=info”
5) Run the django server: “python manage.py runserver” (or gunicorn, whatever you are using).

No comments:

Post a Comment