A containerized framework for distributed, asynchronous tasks with Python Celery (#petitionforaceleryemoji), RabbitMQ 🐰, and Reddis 🥬 (get it? Reddis like "lettuce").
See a more detailed discussion in my post Dockerized Distributed, Asynchronous Work.
- Python 3.9
- Celery==5.0.5
- RabbitMQ (latest Docker image)
- Reddis (latest Docker image)
docker-compose build && docker-compose up -d
Use this framework to jumpstart your distributed and/or asynchronous application! Before use, take note of the following:
- Edit the environment variables accordingly in
docker-compose.yml
:
RABBITMQ_DEFAULT_USER
andRABBITMQ_DEFAULT_PASS
in the rabbitmq container andBROKER_USER
,BROKER_PASS
,BROKER_HOST
, andBROKER_PORT
in the producer and worker (worker1 and worker2) containers are the respective username, password, hostname, and port for the RabbitMQ connection.BACKEND_HOST
andBACKEND_PORT
in the producer and worker (worker1 and worker2) containers are the respective hostname and port for the Redis connection.CONCURRENCY
in the worker (worker1 and worker2) containers is the number of workers running in a single worker docker container concurrently.LOGLEVEL
in the worker (worker1 and worker2) containers is the desired logging level passed to Celery.
- Tasks can be distributed among as many or as few containers as desired, just by adjusting the
scale
value indocker-compose.yml
. In this example, two worker containers are spawned. - The Celery app configuration in the producer (found in
producer/produce.py
) and consumer (found inconsumer/consume.py
) must be the same in order to ensure tasks are being pushed and pulled from the same, correct queue. - Celery tasks can be invoked in multiple ways, one of the most straight forward being to be invoked by name. In this example, we are indeed invoking the
square
task by name. See the task decorator on line 14 inconsumer/consume.py
and the task invocation on line 21 inproducer/produce.py
. - Output from processes managed by supervisord in the producer and worker (worker1 and worker2) containers is all set to be redirected to stdout and stderr. This can be changed to a different file or removed entirely in
producer/conf/supervise-producer.conf
andconsumer/conf/supervise-consumer.conf
.