To the point where Python's Celery and RabbitMQ (Docker) work

Start with Docker as a message broker Use RabbitMQ and write up to the point where a simple program using Celery works.

The content is almost the same as the official document, but I changed it to use Docker's RabbitMQ.

RabbitMQ

#Start-up
docker run --rm -d --hostname my-rabbit --name rabbitmq -p 5672:5672 -p 8080:15672 rabbitmq:3-management

#Stop
docker stop rabbitmq

The port used by the 5672. The 8080 (15672) port is a management screen that can be viewed from a browser. (It doesn't have to be this time)

Library installation

pip install celery

Version 4.4.2 has been installed.

Program preparation

tasks.py


from celery import Celery

app = Celery('tasks', backend='amqp', broker='amqp://guest:[email protected]:5672')


@app.task
def add(x, y):
    return x + y

Create it with the name tasks.py.

Run when you're ready.

celery -A tasks worker --loglevel=info

Input and get messages

>>> r = add.delay(4,4)
>>> r
<AsyncResult: 8a80f867-c2f4-47f6-b431-665c624f0ec2>
>>> r.ready()
True
>>> r.get()
8

I was able to confirm that it was working.

Link

Recommended Posts

To the point where Python's Celery and RabbitMQ (Docker) work
Work memo to migrate and update Python 2 series scripts on the cloud to 3 series
I want to visualize where and how many people are in the factory
I managed to solve the situation where Python does not work on Mac
What to do if the latest Jupyter Notebook and nb extensions don't work
Docker x visualization didn't work and I was addicted to it, so I summarized it!
How to use machine learning for work? 01_ Understand the purpose of machine learning
I tried to summarize the logical way of thinking about object orientation.
I tried to automate the face hiding work of the coordination image for wear
The fastest way for beginners to master Python
Excel X Python The fastest way to work
Easy way to check the source of Python modules
To the point where Python's Celery and RabbitMQ (Docker) work