I have a Python API that uses Flask as the framework. I have a server where I have the code. I can run the API from Poetry or Gunicorn
When to Start the server using Gunicorn
In case you need concurrency and high performance, use this command. This method is used for production environment
go to API_BDG folder and execute:
export $(grep -v '^#' ../.env | xargs) && gunicorn -w <WORKERS_QTY> -b <SERVER_IP>:<SERVER_PORT> app:app
This command is composed of 2 parts connected with &&
which means that the second command is executed if the first is successful.
The first part of the command is:
export $(grep -v '^#' ../.env | xargs)
Where:
-
grep -v '^#'
filters out the commented lines from environment variables -
xargs
: takes the remaining lines and converts them into a single line suitable forexport
. -
export $(...)
: sets those variables in the current shell environment so they’re accessible to processes started afterward.
The Second part of the command is:
gunircorn -w <WORKERS_QTY> -b <SERVER_IP>:<SERVER_PORT> app:app
Where:
-
-w <WORKERS_QTY>
is a flag to set the number of workers to run the app -
-b <SERVER_IP>:<SERVER_PORT>
binds server IP to a port -
app:app
means “from the file app.py, import the object named app”—which is usually a Flask or FastAPI app instance.
Start the server using Poetry
In case concurrency and high performance are not necessary, use this commands. It runs API DBG using Flask (single thread)
go to API_BDG folder and execute:
poetry shell
poetry run python3 src/app.py
It looks simpler and it easier to remember. So I not have to take care of concurrency and high performance I use this.
What are your thoughts of this? when do you prefer Gunicorn or Poetry? Do you have other preferred ways to run the APIs?
Lets share opinions and keep going forward!