python -m pip install flask requests celery redis
Because in our implementation Celery uses Redis as a message broker, we need to start a Redis server.
please refer to https://redis.io/docs/install/install-redis/
redis-server
python app.py
This will start a server on port 5000.
From a different terminal, run the following command to start a worker. The worker will listen to the celery queue and execute the tasks.
celery -A app.celery worker --loglevel=info
# curl http://127.0.0.1:5000/start_task/<task-name>
curl http://127.0.0.1:5000/start_task/test
This will create a task and put it in the celery queue. It will return a task id. The task id can be used to query the status of the task and get the result.
The response looks like this:
{
"task_id": "b1b0b1b0-1b0b-1b0b-1b0b-1b0b1b0b1b0b"
}
The client can then poll the server to get the status of the task and the result when the task is finished. Use the task id returned from the previous step.
# curl http://127.0.0.1:5000/task_status/<task-id>
curl http://127.0.0.1:5000/task_status/b1b0b1b0-1b0b-1b0b-1b0b-1b0b1b0b1b0b
The response for a pending task looks like this:
{
"state": "PENDING",
"status": "Pending..."
}
The response for a completed task looks like this:
{
"result": {
"message": "task 'test' says 'hello!'"
},
"state": "SUCCESS"
}
python flush_redis.py
The server uses Flask to handle the requests.
Flask is a micro web framework written in Python.
The server uses Celery to create tasks and put them in the queue.
Celery is an asynchronous task queue based on distributed message passing.
Celery uses Redis as the message broker. Redis is a fully-fledged in-memory data structure store, used as a database, cache and in this case as a message broker.