捕获它们的shell stdout存活的子进程stderr
以下是我想要做的事情:
-python process captures stderr of multiple subprocesses to watch the subprocesses -each subprocess runs on the separate window displaying stdout.
当我使用Popen(命令,stderr = 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(stdout)和fflush(stderr)。
没有测试过这个,但你能做到吗?
import sys
pp = subprocess.Popen(command, stderr = fpw, stdout = sys.stdout)
I82Much,谢谢你的回答和评论。
是的,你对stderr的评论是绝对正确的。 我在windows7中开发。 在linux(ubuntu10)中,下面是简单的:
cmd_line4sub = command + ' 2> ' + ftempname pp = subprocess.Popen(['/usr/bin/xterm','-e',cmd_line4sub],stderr = fpw)
它打开新的shell,打印子进程标准输出。 父进程捕获子进程stderr。 这是我的第一个目标。
如果我能想出如何在Windows中做,我会发布我的答案;)
(我和提问者是同一个用户,但我不能让他们成为同一个帐户......)
链接地址: http://www.djcxy.com/p/77131.html上一篇: capturing stderr of subprocesses with their shell stdout alive