How run a flask app packaged as pex in gunicorn?
Asked Answered
L

2

5

I am planning to package my flask app as a pex for self contained distribution, wondering how can I run the pex in Gunicorn ? or maybe packaging even gunicorn in the executable so when I run the pex, flask app runs in gunicorn . is that even possible ?

Lodger answered 6/11, 2018 at 20:18 Comment(0)
R
5

I assembled an example app showing how to package a Flask application with Pex and run the resulting pex-file with Gunicorn, see here: https://github.com/wolkenarchitekt/pex-flask-gunicorn-example

These are the essential commands to package and run the app:

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

As you can see, Gunicorn is even packaged within the pex-file.

Redo answered 14/2, 2019 at 16:43 Comment(2)
@alwaysprep could you provide an example?Via
@Via Do you use pants for build process? If so go to dist file where .pex file resides. and write: "PEX_SCRIPT=gunicorn ./{pex_file_name}.pex {path_to_your_app}:app" You need to add gunicorn to your 3rdparty.Inaccuracy
T
1

Just an update about @ifischer answer, the (recently introduced) -c aim to put a script as an entry point for 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
Trinitrobenzene answered 1/3, 2019 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.