nayemabs / cattle-web

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

alt text

Livestock weight web server utilizing FastAPI, Celery with RabbitMQ for task queue. RabbitMQ is also used as Celery backend and optional flower for monitoring the Celery tasks.

Download the model

Please download the model from google drive using the following link: https://drive.google.com/drive/folders/1RGUEoX8ywtBOigCRCdIY4KDIfyFaZwUt

Unzip the model directory into the root directory of the project.

FastAPI

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.

The key features are:

Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.

Fast to code: It allows for significant increases in development speed.

Easy: Designed to be easy to use and learn. Less time reading docs. Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs. Robust: Get production-ready code. With automatic interactive documentation. Standards-based: It’s based on the open standards for APIs, OpenAPI and JSON Schema.

Celery

Celery is a simple, flexible, and reliable distributed system to process vast amounts of messages, while providing operations with the tools required to maintain such a system. It’s a task queue with focus on real-time processing, while also supporting task scheduling. It also helps us to handle asynchronous tasks, which is vital for smooth user experiences. With this powerful combo of FastAPI and Celery, we will be able to do things like:

  • Run machine learning models
  • Send bulk emails
  • Process images or PDFs
  • Generate exports of user data
  • Perform backups

RabbitMQ

RabbitMQ is the most widely deployed open source message broker.

Flower

Flower is a real-time web application monitoring and administration tool for Celery.

Architecture

alt text

Setting up the VirtualEnv and install dependencies

Go inside the project folder and execute the below commands. We will use Pipenv to setup the VirtualEnv.

pipenv shell --python 3.9.2
pipenv install -r requirements.txt

Dependencies will be installed from the Pipfile. Python version 3.9.2 is used for this project.

Prerequisite

  1. Python 3.9.2
  2. Pipenv packaging tool
  3. RabbitMQ instance

Serve the Application

gunicorn -k  uvicorn.workers.UvicornWorker main:app  -b 0.0.0.0:9001

Start the Celery process, navigate to the project directory in a new terminal, activate the virtual environment, and then run:

pipenv shell --python 3.9.2
pipenv install -r requirements.txt
celery -A main.celery worker --loglevel=info -Q cattle --concurrency=2

Optionally we can monitor the tasks submitted to Celery. To start the Flower process, Navigate to the project directory in a new terminal, activate the virtual environment, and then run:

pipenv shell --python 3.9.2
pipenv install -r requirements.txt
celery -A main.celery flower --port=5555

Once the Flower starts we can see the submitted tasks at http://localhost:5555/tasks.

This will start the application on port 900` and Celery process will listen to the Queue cattle

Test the application

FastAPI also automatically generated fully interactive API documentation that we can use to interact with our API. We can visit http://127.0.0.1:9001/docs in our browser to see the interactive API documentation provided by Swagger UI:

alt text

The server will start at http://localhost:9000/docs.

Please check out the article for further details. Async Architecture with FastAPI, Celery, and RabbitMQ

References

About

License:MIT License


Languages

Language:Python 99.9%Language:Dockerfile 0.1%Language:Shell 0.0%