Python使用Supervisor来管理进程的方法

yipeiwu_com5年前Python基础

本文实例讲述了Python使用Supervisor来管理进程的方法。分享给大家供大家参考。具体分析如下:

Supervisor可以启动、停止、重启*nix系统中的程序。也可以重启崩溃的程序。

supervisord的一个守护进程,用于将指定的进程当做子进程来运行。

supervisorctl是一个客户端程序,可以查看日志并通过统一的会话来控制进程。

看例子:

我们写了一个py脚本,用于往log文件中记录一条当前的时间。

root@ubuntu:/home/zoer# cat daemon.py
#!/usr/bin/env python
import time
import os
time.sleep(1)
f=open("log",'a')
t=time.time()
f.write(str(t))
f.write("\n")
f.close()

安装过程就不说了。

安装完毕supervisor之后【将配置文件放在/etc下】。修改配置文件,在最后增加如下内容:

[program:ddd]
command=/home/zoer/daemon.py
autorestart=true

然后我们启动supervisor并启动daemon.py的执行。

root@ubuntu:/home/zoer# supervisord 
/usr/local/lib/python2.7/dist-packages/supervisor-3.0b1-py2.7.egg/supervisor/options.py:286: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security. 
 'Supervisord is running as root and it is searching ' 
root@ubuntu:/home/zoer# supervisorctl 
ddd               STARTING   
supervisor> start ddd 
ddd: ERROR (already started) 
supervisor> stop ddd 
ddd: stopped 
supervisor> start ddd 
ddd: started 
supervisor>

从上面的例子中,看到,可以通过start或者stop命令来启动或者停止ddd这个进程。ddd这里就是我们在配置文件中增加的内容(daemon.py这个脚本)。

也可以使用restart。如下:

supervisor> restart  ddd
ddd: stopped
ddd: started

下面我们测试一下,假设说我们手动kill掉了ddd这个进程,那么ddd会自动恢复执行吗?

为了做实验,把代码修改如下:

root@ubuntu:/home/zoer# cat daemon.py  
#!/usr/bin/env python 
import time 
import os 
while True: 
  time.sleep(1) 
  f=open("log",'a') 
  t=time.time() 
  f.write(str(t)) 
  f.write("\n") 
  f.close()

通过ps可以找到这个进程的id:

root   9354 0.2 0.4 10924 4200 ?    S  23:16  0:00 python /home/zoer/daemon.py 
root   9395 0.0 0.0  4392  832 pts/3  S+  23:17  0:00 grep --color=auto daemon 
root@ubuntu:/home/zoer#

看下面的操作:

root@ubuntu:/home/zoer# rm log;touch log;kill 9354
root@ubuntu:/home/zoer# cat log 
1364710712.51 
root@ubuntu:/home/zoer# cat log 
1364710712.51 
1364710713.51 
root@ubuntu:/home/zoer# cat log 
1364710712.51 
1364710713.51 
root@ubuntu:/home/zoer# cat log 
1364710712.51 
1364710713.51 
1364710714.52 
root@ubuntu:/home/zoer# cat log 
1364710712.51 
1364710713.51 
1364710714.52 
1364710715.52

删除了log文件,并且重新创建。然后干掉了daemon.py的那个进程。会发现log内容又重新有新的内容了。再次ps查看进程号。

root   9429 0.1 0.4 10924 4200 ?    S  23:18  0:00 python /home/zoer/daemon.py 
root   9440 0.0 0.0  4392  828 pts/3  S+  23:19  0:00 grep --color=auto daemon 
root@ubuntu:/home/zoer#

会发现进程号已经变成9429了。说明supervisor已经重启了被干掉了的进程。

希望本文所述对大家的Python程序设计有所帮助。

相关文章

对Python Class之间函数的调用关系详解

假设有Class A 和 Class B两个类,Class A中定义了a(),Class B中定义了b(). 现在我想在Class B中调用 Class A中的函数a()。此处介绍三种调...

python 3.7.0 安装配置方法图文教程

python 3.7.0 安装配置方法图文教程

本文记录了python 3.7.0 安装配置方法,供大家参考,具体内容如下 S1 登入Python官网下载网址 S2 下载后缀为exe的可执行文件,并根据自己电脑/主机的系统选择32位还...

Python RuntimeError: thread.__init__() not called解决方法

在写一个多线程类的时候调用报错 RuntimeError: thread.__init__() not called 复制代码 代码如下: class NotifyTread(thre...

Python初学者需要注意的事项小结(python2与python3)

一、注意你的Python版本 Python官方网站为http://www.python.org/,当前最新稳定版本为3.6.5,在3.0版本时,Python的语法改动较大,而网上的不少教...

Tensorflow 自带可视化Tensorboard使用方法(附项目代码)

Tensorflow 自带可视化Tensorboard使用方法(附项目代码)

Tensorboard: 如何更直观的观察数据在神经网络中的变化,或是已经构建的神经网络的结构。上一篇文章说到,可以使用matplotlib第三方可视化,来进行一定程度上的可视化。然而T...