django development - building distributed (multi node) task queue with cellery

Today, I'll show you how to use cellery to build a task queue with two nodes in django project (one master node and one child node; the master node publishes the task, and the child node receives the task and executes it. It's similar to setting up three or more nodes), using cellery, rabbitmq. The knowledge in cellery and rabbitmq will not be introduced here alone.

1. Basic environment of the project:
Two Ubuntu 18.04 virtual machines, Python 3.6.5, Django 2.0.4, cellery 3.1.26post2

2. Main node django project structure:

3. Configuration of cellery in

import djcelery
# Both Queue and Exchange here involve the concepts in RabbitMQ, which will not be introduced here
from kombu import Queue, Exchange
BROKER_URL = 'amqp://test:test@'
CELERY_RESULT_BACKEND = 'amqp://test:test@'

# CELERY_ACCEPT_CONTENT = ['json', 'pickle', 'msgpack', 'yaml']


CELERY_IMPORTS = ("proj.celery1.tasks", )

  Queue('train', routing_key='train'),
  Queue('predict', routing_key='predict'),

4. Configuration in

# coding:utf8
from __future__ import absolute_import

import os

from celery import Celery
from django.conf import settings

# set the default Django settings module for the 'celery' program.

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')

app = Celery('proj')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
# app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

def debug_task(self):
    print('Request: {0!r}'.format(self.request))

5. Configuration in proj /

from __future__ import absolute_import
from .celery import app as celery_app

6. Cellery1 / (tasks in the primary node will not be executed, only tasks in the child nodes will be executed)

from __future__ import absolute_import
from celery import task

def do_train(x, y):
    return x + y


from .tasks import do_train
class Test1View(APIView):
    def get(self, request):
            # The queue and routing Φ key here also involve knowledge in RabiitMQ
            # Key. Here, it controls which queue to send tasks to. The sub nodes execute the tasks in the corresponding queue through this
            ret = do_train.apply_async(args=[4, 2], queue="train", routing_key="train")
            # Get results
            data = ret.get()
        except Exception as e:
            return Response(dict(msg=str(e), code=10001))
        return Response(dict(msg="OK", code=10000, data=data))

8. Sub node directory structure:

9. In the child node, cellery1 /

from __future__ import absolute_import
from celery import Celery
CELERY_IMPORTS = ("celery1.tasks", )
app = Celery('myapp',
             # This involves the knowledge of RabbitMQ, which corresponds to the master node


if __name__ == '__main__':

10. In the child node, cellery1 /

from __future__ import absolute_import
from kombu import Queue,Exchange
from datetime import timedelta

CELERY_ACCEPT_CONTENT = ['json','pickle','msgpack','yaml']

# exchange type can see relevant contents in RabbitMQ


11. Child node cellery1 / (this is the task to be executed, each node can be different)

from __future__ import absolute_import
from celery1.celery import app

import time
from celery import task

def do_train(x, y):
    :param data:
    return dict(data=str(x+y),msg="train")

12. Start the cellery in the child node:
Cellery1 is a project, - Q train means to receive tasks from train

celery -A celery1 worker -l info -Q train

13. Start the django project in the master node:

python runserver

14. Use Postman to request the corresponding view

Request url:
The returned result is:
    "msg": "OK",
    "code": 10000,
    "data": {
        "data": "6",
        "msg": "train"

15. Problems encountered:
1) cell queue error: AttributeError: 'str' object has no attribute 'items'
Solution: return the redis library from 3.0 to 2.10, pip install redis==2.10
Solution reference link:

Let's talk about it today. If you have any questions, welcome to exchange.

Tags: Linux Celery Django JSON RabbitMQ

Posted on Mon, 02 Dec 2019 19:43:06 -0500 by !jazz