When revisiting something gets me to the verge of tears, I know I have to write a small essential guide for myself. Celery is among these things. Here is part one. Contents below!
Next part: Part 2 🍮 Results pudding
Following stack: Django + Celery + Nginx
Project structure
proj
proj
__init__.py
settings.py
urls.py
...
celery.py
firstapp
tasks.py
Content table
🍜 Cooking project dinner
- 🥩 Django steak medium rare
- 🥗 Broker salad
- 🥬 Chopped Redis
- 🥕 Carrot for Rabbitmq
- 🥒 Pickled tasks
- 🥗 Broker salad
- 🍮 Results pudding
- 🍪 HTML
- 🍯 View
- 🍫 Url
- 🥄 Testing the taste
- 🍻 Serving Celery workers
- 🍱 Pack Linux with Celery Daemon
🍜 Cooking project dinner
Install celery in your environment:
pip install celery
🥩 1. Django steak medium rare
Create a proj/proj/celery.py
file:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
app = Celery('proj')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
To proj/proj/__init__.py
add:
from .celery import app as celery_app
__all__ = ['celery_app']
For a broker I will use RabbitMQ. And for results backend I will show two options – Redis (with somewhat my results page) and DB.

– Nah, it won’t…
🥗 1.1 Broker
For broker there are two main options: Redis and RabbitMQ.
🥬 1.1.1 Chopped Redis
Run these commands in your environment:
pip install redis
redis-server
To settings.py add:
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'django-db'
🥕 1.1.2 Carrots for Rabbitmq
Run these commands in your environment:
sudo apt-get install -y erlang
sudo apt-get install rabbitmq-server
systemctl enable rabbitmq-server
systemctl start rabbitmq-server
systemctl status rabbitmq-server
To settings.py add:
CELERY_BROKER_URL = 'amqp://localhost'
CELERY_RESULT_BACKEND = 'django-db'
🥒 1.2 Pickled tasks
In the proj/proj/firstapp add a tasks.py with the following content:
from __future__ import absolute_import, unicode_literals
from proj.celery import app
@app.task
def add(x, y):
res = x + y
print(res)
Next part: Part 2 🍮 Results pudding
[…] Previous part: Part 1 🥩 Django steak medium rare […]
LikeLike