Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PM2.js to Run Gunicorn/Flask App inside Virtualenv/Anaconda env

I have been running gunicorn to serve a Python Flask app using the commands

conda activate fooenv
gunicorn --workers=4 -b 0.0.0.0:5000 --worker-class=meinheld.gmeinheld.MeinheldWorker api.app:app 

How can we use pm2 instead to run gunicorn/flask app inside the fooenv environment?

like image 216
Athena Wisdom Avatar asked Oct 29 '25 08:10

Athena Wisdom


2 Answers

supposed you can run gunicorn in your venv via e.g.:

gunicorn wsgi:app -b localhost:5010

then you simply use command (in the venv):

pm2 --name=myapp start "gunicorn wsgi:app -b localhost:5010"

(took me way too long to figure this out btw)

like image 131
Moe Singh Avatar answered Oct 31 '25 00:10

Moe Singh


I would create a pm2.json file in the same directory as your app, then start it with pm2 start pm2.json. pm2 ls will now show your app running.

{
    "apps": [
      {
        "name": "my-app",
        "script": "gunicorn --workers=4 -b 0.0.0.0:5000 --worker-class=meinheld.gmeinheld.MeinheldWorker api.app:app",
        "watch": false,
        "max_memory_restart": "256M",
        "output": "/var/www/html/logs/my-app-out.log",
        "error": "/var/www/html/logs/my-app-error.log",
        "kill_timeout": 5000,
        "restartDelay": 5000
      }
    ]
  }
like image 41
xinthose Avatar answered Oct 30 '25 23:10

xinthose