如何为nodejs运行像pm2这样的python脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32127834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a python script like pm2 for nodejs
提问by toy
I've used pm2
for my Node.js script and I love it.
Now I have a python script which collect streaming data on EC2. Sometimes the script bombs out and I would like a process manager to restart itself like pm2.
我已经用于pm2
我的 Node.js 脚本并且我喜欢它。
现在我有一个 python 脚本,它在 EC2 上收集流数据。有时脚本会爆炸,我希望进程管理器像 pm2 一样重新启动自己。
Is there something the same as pm2 for python? I've been searching around and couldn't find anything.
有没有与python的pm2相同的东西?我一直在四处寻找,找不到任何东西。
Here's my error
这是我的错误
File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 430, in filter
self._start(async)
File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 346, in _start
self._run()
File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 286, in _run
raise exception
AttributeError: 'NoneType' object has no attribute 'strip'
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90:
It's a simple data collecting script
这是一个简单的数据收集脚本
class StdOutListener(StreamListener):
def on_data(self, data):
mydata = json.loads(data)
db.raw_tweets.insert_one(mydata)
return True
def on_error(self, status):
mydata = json.loads(status)
db.error_tweets.insert_one(mydata)
if __name__ == '__main__':
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
#This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
stream.filter(follow=[''])
That I would like it to just restart itself in case something happens.
我希望它在发生某些事情时重新启动。
采纳答案by abcdn
UPD: See answers below for better solutions.
UPD:请参阅下面的答案以获得更好的解决方案。
--
——
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.
有几种解决方案。首先,您可以使用http://supervisord.org/,这是一个不错的通用过程控制系统,它包括许多开箱即用的功能,例如自动重启、重启计数器、日志记录、灵活配置等。
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.
除此之外,您可以将实现逻辑包装到一个函数中,在try except
块中运行它,捕获所有异常,当出现异常时,再次运行该函数而不是退出脚本。在您的情况下,此类功能可能包括创建侦听器、身份验证和流部分。
回答by Simon Smith
You can actually run python scripts from within pm2:
你实际上可以在 pm2 中运行 python 脚本:
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:
如果脚本以 .py 后缀结尾,它将默认使用 python 解释器。如果您的文件名不以 .py 结尾,您可以执行以下操作:
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.
我发现您必须小心使用哪种 python,特别是如果您使用的 virtualenv python 与机器上的“默认”python 版本不同。
回答by Nokey
PM2 is enough, it will run interpreter by suffix:
PM2 就够了,它会通过后缀运行解释器:
{
".sh": "bash",
".py": "python",
".rb": "ruby",
".coffee" : "coffee",
".php": "php",
".pl" : "perl",
".js" : "node"
}
回答by u1389917
In my case I use scrapyd in my project. The original command is:
就我而言,我在我的项目中使用了 scrapyd。原来的命令是:
scrapyd --pidfile /var/log/scrapyd/twistd.pid -l /var/log/scrapyd/logs/scrapyd.log
scrapyd --pidfile /var/log/scrapyd/twistd.pid -l /var/log/scrapyd/logs/scrapyd.log
and the pm2 version is:
pm2 版本是:
pm2 start scrapyd --interpreter python --watch --name=scrapyd -- --pidfile "/var/log/scrapyd/twistd.pid" -l "/var/log/scrapyd/logs/scrapyd.log"
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
希望这个例子可以帮助
回答by Saiful
I created a echosystem file ecosystem.config.json
我创建了一个 echosystem 文件 ecosystem.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 服务:
$ pm2 start ecosystem.config.json
$ pm2 -v
3.2.8
回答by anarchist912
PM2 with pipenv
带pipenv的PM2
For those trying to run a python program from/with pipenvtry a pm2.config.json(or ecosystem.json.config as in the official documentation of PM2) like this:
对于那些试图从/使用pipenv运行 python 程序的人,请尝试使用pm2.config.json(或 PM2 的官方文档中的生态系统.json.config),如下所示:
The important parts being "interpreter" : "pipenv"
and "interpreter_args": "run python3"
.
重要的部分是"interpreter" : "pipenv"
和"interpreter_args": "run python3"
。
pm2.config.json
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.
然后pm2 start pm2.config.json
。pm2 delete BackupService
在重新开始之前,我总是(或您在“名称”中称呼它的任何名称),因为即使使用--update-env
标志,它也不会使用更新的pm2.config.json
. 不知道为什么。
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 文档,"interpreter_args"
, 似乎已更改为"node_args"
。我正在运行pm2 --version
3.0.0,旧方式仍然有效。
PM2 with Python multiprocessing
带有 Python 多处理的 PM2
If you want to run a python program that uses Pythons multiprocessing lib, the solution is to force running it in forkmode. PM2, if not told otherwise, automatically tries to run it in cluster
mode, it seems.
如果要运行使用 Python 多处理库的 Python 程序,解决方案是强制以fork模式运行它。PM2,如果没有另外说明cluster
,它似乎会自动尝试在模式下运行它。
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.
但是,我怀疑,我们需要将多处理部分完全留给 Python。我无法想象 PM2 能够管理由 Python 的多处理产生的多个进程——它在cluster
模式下运行时会尝试这样做。此外,根据 PM2 文档,当使用该"interpreter"
选项(例如用于 pipenv)时,仅fork_mode
会起作用。
So add "exec_mode": "fork"
to your pm2.config.json
to make it run.
所以添加"exec_mode": "fork"
到你的pm2.config.json
让它运行。
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)
如果你不使用pm2.config.json
文件,传递-i 0
到也pm2 start
应该强制分叉模式。(-i 代表实例)
回答by Ankit Kumar Rajpoot
You can use nohup- Nohup, short for no hang-up is a command in Linux systems that keep processes running even after exiting the shell or terminal. Nohup prevents the processes or jobs from receiving the SIGHUP (Signal Hang UP) signal. This is a signal that is sent to a process upon closing or exiting the terminal. Some basics nohup's commands are given below.
您可以使用 nohup - Nohup 是 no hang-up 的缩写,是 Linux 系统中的一个命令,即使在退出 shell 或终端后也能保持进程运行。Nohup 阻止进程或作业接收 SIGHUP(信号挂起)信号。这是在关闭或退出终端时发送到进程的信号。下面给出了一些基本的 nohup 命令。
nohup mycommand
OR
nohup python3 -m flask run &