How to run a python script like pm2 for nodejs

不问归期 提交于 2019-11-30 04:48:08

UPD: See answers below for better solutions.

--

There are several solutions for that. First, you may use http://supervisord.org/ which is a decent universal process controll system, which includes a lot of features out of the box, such as autorestart, restart counter, logging, flexible configuration and more.

Beyond that, you may just wrap your implementation logic into a function, run it within try except block, catch all exceptions and when an exception is cought, run the function again instead of exiting the script. In your case such function might include creating listener, authentication and stream part.

You can actually run python scripts from within pm2:

pm2 start echo.py

If the script ends in a .py suffix it will use a python interpreter by default. If your filename doesn't end in .py you can do:

pm2 start echo --interpreter=python

I've found you have to be a little bit careful which python you are using, especially if you are using a virtualenv python with a different version to the 'default' python on your machine.

PM2 is enough, it will run interpreter by suffix:

{
  ".sh": "bash",
  ".py": "python",
  ".rb": "ruby",
  ".coffee" : "coffee",
  ".php": "php",
  ".pl" : "perl",
  ".js" : "node"
}

In my case I use scrapyd in my project. The original command is:

scrapyd --pidfile /var/log/scrapyd/twistd.pid -l /var/log/scrapyd/logs/scrapyd.log

and the pm2 version is:

pm2 start scrapyd --interpreter python --watch --name=scrapyd -- --pidfile "/var/log/scrapyd/twistd.pid" -l "/var/log/scrapyd/logs/scrapyd.log"

hope this example can help

I created a echosystem file echosystem.config.json

{
    "apps": [{
        "name": "app_name",
        "script": "/the/app/path/my_app.py",
        "args": ["-c", "my_config.prod.json"],
        "instances": "1",
        "wait_ready": true,
        "autorestart": false,
        "max_restarts": 5,
        "interpreter" : "/path/to/venv/bin/python",
    }]
}

Run the pm2 service:

$ pm2 start echosystem.config.json
$ pm2 -v
3.2.8

PM2 with pipenv

For those trying to run a python program from/with pipenv try a pm2.config.json (or ecosystem.json.config as in the official documentation of PM2) like this:

The important parts being "interpreter" : "pipenv" and "interpreter_args": "run python3".

pm2.config.json

{
    "apps": [{
        "name": "BackupService",
        "script": "/home/service-backup/service/server.py",
        "args": [""],
        "wait_ready": true,
        "autorestart": false,
        "max_restarts": 5,
        "interpreter" : "pipenv",
        "interpreter_args": "run python3"
    }]
}

Then pm2 start pm2.config.json. I always pm2 delete BackupService (or whatever you call it in "name"), before starting again, because even with the --update-env flag it does not make use of a updated pm2.config.json. Don't know why.

Also note that "interpreter_args", seems to have been changed to "node_args", according to the latest PM2 docs. I am running pm2 --version 3.0.0, and the old way still works.

PM2 with Python multiprocessing

If you want to run a python program that uses Pythons multiprocessing lib, the solution is to force running it in fork mode. PM2, if not told otherwise, automatically tries to run it in cluster mode, it seems.

However, I suspect, we need to leave the multiprocessing part to Python completely. I can't imagine PM2 being able to manage the multiple processes being spawned by the multiprocessing of Python — which it tries, when running in cluster mode. Also, when using the "interpreter" option (e.g. for pipenv), only fork_mode will work, according to the PM2 docs.

So add "exec_mode": "fork" to your pm2.config.json to make it run.

If you don't use a pm2.config.json file, passing -i 0 to pm2 start should force fork mode as well. (-i stands for instances)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!