以下是我要做的事情:
-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.
我想要两个“好”的。我能为此做些什么?提前致谢。 (目前,我在windows7中使用python3.2开发)
这是用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)。
答案 0 :(得分:0)
没有测试过这个,但你能做到吗
import sys
pp = subprocess.Popen(command, stderr = fpw, stdout = sys.stdout)
答案 1 :(得分:0)
I82很多,谢谢你的回答和评论。
是的,你对stderr的评论是完全正确的。我在windows7中开发。在linux(ubuntu10)中,以下简单解决了:
cmd_line4sub = command + ' 2> ' + ftempname pp = subprocess.Popen(['/usr/bin/xterm','-e',cmd_line4sub],stderr = fpw)
它打开新的shell,打印子进程标准输出。父进程捕获子进程stderr。这是我的第一个目标。
如果我能弄清楚如何在Windows中做,我会发布我的答案;)
(我与提问者的用户相同,但我不能将它们设为同一帐户......)