简体   繁体   English

如何监视 Python 进程并在它死掉时重新启动?

[英]How can I monitor a Python process and relaunch if it dies?

I have a program that I'm running that listens to a queue (it's not multi-threaded so I want to run several instances of it).我有一个正在运行的程序,它侦听队列(它不是多线程的,所以我想运行它的多个实例)。 I've tried my best to catch errors but in the event the app crashes due to a error or bad incoming data, I want to be able to respawn the Python app (after I log the stacktrace) so it continues to work.我已尽力捕捉错误,但如果应用程序因错误或传入数据错误而崩溃,我希望能够重新生成 Python 应用程序(在我记录堆栈跟踪之后),以便它继续工作。

I felt this might be a common problem for people who run python based services so I thought I would ask but I was thinking of writing some code to do a ps -ef and count the instances of the name of the Python program (if less than a threshold then I would have the program relaunch it).我觉得这对于运行基于 Python 的服务的人来说可能是一个常见问题,所以我想我会问,但我正在考虑编写一些代码来执行ps -ef并计算 Python 程序名称的实例(如果小于一个阈值,然后我会让程序重新启动它)。

Before I build this, I wanted to know if there was perhaps a better way or a existing tool/module that did this?在我构建它之前,我想知道是否有更好的方法或现有的工具/模块可以做到这一点?

Checkout supervisord .结帐主管 I use it regularly to launch, monitor all types of things.我经常用它来启动、监控所有类型的事情。

Here is how I set it up to launch a wsgi app on my server:这是我如何设置它以在我的服务器上启动 wsgi 应用程序:

[program:quizzes]
directory = /var/www/quizzes.seasources.net
command = /home/jaime/code/virtualenv/quizzes/bin/uwsgi uwsgi.ini
process_name = quizzes
autostart = true
startsecs = 5
user = www-data
redirect_stderr = true
stdout_logfile = /var/www/quizzes.seasources.net/logs/supervisor-console.log
environment = PYTHON_EGG_CACHE=/tmp/python-eggs

The configuration file format is easy to understand and it even logs stdout/stderr to a file.配置文件格式易于理解,它甚至将 stdout/stderr 记录到文件中。 Above it's /var/www/quizzes.seasources.net/logs/supervisor-console.log You can read more about configuration here .上面是 /var/www/quizzes.seasources.net/logs/supervisor-console.log 您可以在此处阅读有关配置的更多信息。

You could use a supervisor.您可以使用主管。 A well known one that's written in Python would be supervisord , a more recent one also in Python would be Circus , and then there are Monit or daemontools and probably many more.一个众所周知的用 Python 编写的将是supervisord ,一个更新的 Python 也是Circus ,然后是 Monit 或 daemontools ,可能还有更多。

If you are looking for something more simple, you can use the subprocess module (python default) to start and check your processes... A basic version would look like this:如果您正在寻找更简单的东西,您可以使用 subprocess 模块(python 默认)来启动和检查您的流程......基本版本如下所示:

# run.py

import subprocess, time

# add your listener processor call here
_PROCESS_ARGS = ['python','/path/to/listener.py']
_PROCESS_TOTAL = 10

process_list = []

# start the processes...
for i in range(_PROCESS_TOTAL):     
    process_list.append(subprocess.Popen(_PROCESS_ARGS))

while True:     
    for i in range(_PROCESS_TOTAL):         
        p = process_list[i]         
            if p.poll() != None: # check if process is running                      
                process_list[i] = subprocess.Popen(_PROCESS_ARGS) # if not, replace with new one
    time.sleep(1) # check only every second...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM