To Gunicorn or to Poetry, That is The Question
介绍了使用Flask框架的Python API启动方法,包括Gunicorn(适用于高并发、高性能生产环境)和Poetry(适用于开发或测试环境)。 2025-7-16 05:28:21 Author: hackernoon.com(查看原文) 阅读量:17 收藏

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 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 <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!


文章来源: https://hackernoon.com/to-gunicorn-or-to-poetry-that-is-the-question?source=rss
如有侵权请联系:admin#unsafe.sh