An example demonstrates how to perform heavy and long-time background computation tasks(such as running a machine learning model) in microservices.
This repo was built up with two services: API service and Worker service.
Service tech stack:
- FastAPI (api server)
- Celery (worker)
- PostgresSQL (persist task in the database)
- Redis (message broker)
API Service
- REST API
- create task
- query task
- Message Queue
- create task
- query task Worker Service
- Message Queue
- run task
- send task progress
Workflow:
NOTE:
Task
is defined by a custom FastAPIApp
frontend user.Celery Task
is defined byCelery
for internal- Update
Celery Task
progress with custom states
docker run -it --rm fastapi-celery-web bash
docker exec -it fastapi-celery-db-1 bash
docker-compose build
# CTRL+C: just stop containers while keeping data
docker-compose up
docker-compose up -d
# Remove containers with wiping data
docker-compose down
NOTE:
Dockerfile.dev
inapp
folder is used for development in a container environment. It builds avirtual environment
with all dependencies installed to let you use such ahot reload
feature when coding.