我正在寻找一种安全的方式来重新启动我的 python 程序,该程序能够重新控制在重新启动之前启动的子进程。
我使用带有线程的子进程来监视stdout
/stderr
的长期运行command
,这将不断生成一些输出消息。
示例代码片段如下:
class PS(threading.Thread):
def __init__(self, command):
threading.Thread.__init__(self)
self.command = command
def run(self):
try:
process = subprocess.Popen(self.command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in iter(process.stdout.readline, ''):
''' do something to line'''
当我的 python 主程序终止时(例如杀死它的 pid),它的子进程(command
由PS
类执行)作为后台进程仍然存在。
我的问题是,python 中是否有任何方法可以“重新附加”子进程,以便我可以继续监视它的stdout
/ stderr
?
PS我只需要在linux环境下,更具体地说是在ubuntu 14.04中。