开发者

Execute and monitor multiple instances of external program in Python

开发者 https://www.devze.com 2022-12-09 16:10 出处:网络
Main program is like this: PREPARE PARAMETERS FOR CHILD PROCESSES subprocess.Popen(\'python child.py param=example1\'.split(\' \'))

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 instanc开发者_如何学Pythones 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.


Keep a dict with the .pids of the child processes as keys, and the commandlines to restart them as corresponding values. i.e.:

childid = []
for cmdline in cmdlines:
  p = subprocess.Popen(cmdline.split())
  childid[p.pid] = cmdline

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.:

while mustcontinue:
  pid, exitstat = os.wait()
  cmdline = childid.pop(pid)
  p = subprocess.Popen(cmdline.split())
  childid[p.pid] = cmdline

Presumably you have some criteria for when this infinite loop ends, I just used mustcontinue as the name for those criteria here;-).

0

精彩评论

暂无评论...
验证码 换一张
取 消