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 ?

user2616355
  • 423
  • 1
  • 4
  • 15

2 Answers2

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.

Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174
  • @alwaysprep could you provide an example? – Germaniero Jun 24 '19 at 14:49
  • 1
    @Germaniero 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. – alwaysprep Jun 26 '19 at 13:06
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
Jérôme B
  • 420
  • 1
  • 6
  • 25