Celery + Django tutorial (Part 1). 🥩 Django steak medium rare

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

  1. 🥩 Django steak medium rare
    1. 🥗 Broker salad
      1. 🥬 Chopped Redis
      2. 🥕 Carrot for Rabbitmq
    2. 🥒 Pickled tasks
  2. 🍮 Results pudding
    1. 🍪 HTML
    2. 🍯 View
    3. 🍫 Url
  3. 🥄 Testing the taste
  4. 🍻 Serving Celery workers
  5. 🍱 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.

– It’s gonna fucking blow up.
– 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

One thought on “Celery + Django tutorial (Part 1). 🥩 Django steak medium rare

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.