Price Prediction

To Gunicorn or to Poetry, That is The Question

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  -b : 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 for export.

  • 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  -b : app:app

Where:

  • -w is a flag to set the number of workers to run the app

  • -b : 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!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button