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)
pip install celery
Version 4.4.2 has been installed.
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
>>> 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.
Recommended Posts