这里有事情,我试图做的:蟒蛇 - 捕捉与它们的壳子进程的标准错误标准输出活着
-python process captures stderr of multiple subprocesses to watch the subprocesses -each subprocess runs on the separate window displaying stdout.
当我使用POPEN(命令,标准错误= fp4tempfile),
(good) the python process can capture stderr of the subprocesses (bad) the subprocess shells stop displaying stdout.
当我使用POPEN(命令),
(good) each subprocess shells displays stdout (as well as stderr, which does not matter for me), (bad) the python process cannot capture stderr.
我想既“好”。我能为此做些什么?提前致谢。 (目前,我使用python3.2在Windows7开发)
这里是用Python编写的父进程来源:
import os,subprocess
import time
fpws = []
fprs = []
def run_command(command):
<specifying a file to write -- skipped>
fpw = open(ftempname,'w')
fpr = open(ftempname,'r')
#cmd_redirect = "%s 2>%s" % (command,ftempname)#didnt do anything
#starting a sub-program:
pp = subprocess.Popen(command,stderr = fpw) #++
#pp = subprocess.Popen(command) #@@
fpws.append(fpw)
fprs.append(fpr)
def watch_program():
while True: #running forever for simplfication
for fpr in fprs:
outchunk = fpr.read()
<do something for subprocesses stderr -- skipped>
time.sleep(1.0)
if __name__ == '__main__':
cmd1 = '(path to program1)'
cmd2 = '(path to program2)'
run_command(cmd1) #kicking cmd1
run_command(cmd2) #kicking cmd2
watch_program() #hearing stderr msg from the other process
注:在子侧,fflush(标准输出)和fflush(错误)根据需要调用。
编辑:没有注意到子进程是在不同的窗口。不知道为什么更改stderr会对输出到stdout的内容产生任何影响。你确定输出消失的原因是它实际上是错误的吗? – I82Much 2011-03-24 15:50:43