2011-03-23 20 views
1

这里有事情,我试图做的:蟒蛇 - 捕捉与它们的壳子进程的标准错误标准输出活着

 
-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(错误)根据需要调用。

回答

0

没有测试过这一点,但你可以做

import sys 

pp = subprocess.Popen(command, stderr = fpw, stdout = sys.stdout) 
+0

编辑:没有注意到子进程是在不同的窗口。不知道为什么更改stderr会对输出到stdout的内容产生任何影响。你确定输出消失的原因是它实际上是错误的吗? – I82Much 2011-03-24 15:50:43

0

I82Much,谢谢你的回答和评论。

是的,您对stderr的评论是完全正确的。我在windows7中开发。在Linux操作系统(ubuntu10),下面简单地计算出:

 
    cmd_line4sub = command + ' 2> ' + ftempname 
    pp = subprocess.Popen(['/usr/bin/xterm','-e',cmd_line4sub],stderr = fpw) 

它打开新的弹,印刷子标准输出。父进程捕获子进程stderr。这是我的第一个目标。

如果我所能想出如何在Windows中执行,我会后我的答案;)

(我是同一个用户的提问,但我不能让他们在同一帐户... )

相关问题