在Python中执行和监控外部程序的多个实例
主程序是这样的:
PREPARE PARAMETERS FOR CHILD PROCESSES
subprocess.Popen('python child.py param=example1'.split(' '))
subprocess.Popen('python child.py param=example2'.split(' '))
...
如何使主程序监视它启动的子进程的每个实例,并在子进程未运行时使用相应的参数重新启动它。
保持子进程的多个实例运行而不是在主进程中实现多线程架构的目的是利用尽可能多的 CPU 和数据库吞吐量。
Main program is like this:
PREPARE PARAMETERS FOR CHILD PROCESSES
subprocess.Popen('python child.py param=example1'.split(' '))
subprocess.Popen('python child.py param=example2'.split(' '))
...
How to make main program to monitor each instances of child process it launched and restart it with its corresponding parameters if it's not running.
The purpose for keep multiple instances of child process running instead of implementing a multi-thread architect within main process is to utilize as much CPU and database throughputs as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
保留一个字典,其中子进程的 .pid 为键,重启子进程的命令行为相应的值。即:
os.wait 将在任何时候返回子进程终止:它为您提供子进程的 (pid, exitstatus)。因此,只需适当重启并维护
childid
即可。即:大概您对这个无限循环何时结束有一些标准,我只是在这里使用
mustcontinue
作为这些标准的名称;-)。Keep a dict with the
.pid
s of the child processes as keys, and the commandlines to restart them as corresponding values. i.e.:os.wait will return whenever any child process terminates: it gives you (pid, exitstatus) of the child. So just restart appropriately and maintain
childid
. i.e.:Presumably you have some criteria for when this infinite loop ends, I just used
mustcontinue
as the name for those criteria here;-).