运行 flask 应用程序如何在 gunicorn 中打包为 pex?

How run a flask app packaged as pex in gunicorn?

我打算将我的 flask 应用程序打包为一个 pex 以进行独立分发,想知道如何 运行 Gunicorn 中的 pex?或者甚至可以将 gunicorn 打包到可执行文件中,所以当我 运行 pex 时,flask 应用程序 运行s 在 gunicorn 中。这可能吗?

我组装了一个示例应用程序,展示了如何使用 Pex 打包 Flask 应用程序以及 运行 使用 Gunicorn 生成的 pex 文件,请参见此处: https://github.com/wolkenarchitekt/pex-flask-gunicorn-example

这些是打包和 运行 应用程序的基本命令:

python setup.py sdist
pex -vv -r requirements.txt -D . -o ./hello-service.pex
PEX_SCRIPT=gunicorn ./hello-service.pex hello_service.main:app -b :8000

如您所见,Gunicorn 甚至打包在 pex 文件中。

只是关于@ifischer 回答的更新,(最近引入的)-c 旨在将脚本作为 pex 的入口点

pip3 wheel -w . .
pex --python=python3 -r requirements.txt -f . -o hello-service.pex -c gunicorn
./hello-service.pex hello_service.main:app -b :8000