capturing stderr of subprocesses with their shell stdout alive
Here are things I'm trying to do:
-python process captures stderr of multiple subprocesses to watch the subprocesses -each subprocess runs on the separate window displaying stdout.
When I use Popen(command,stderr = fp4tempfile) ,
(good) the python process can capture stderr of the subprocesses (bad ) the subprocess shells stop displaying stdout.
When I use Popen(command) ,
(good) each subprocess shells displays stdout (as well as stderr, which does not matter for me), (bad ) the python process cannot capture stderr.
I want both "good"s. What can I do for this? Thanks in advance. (currently, I'm using python3.2 developing in windows7)
Here's the parent process source written in 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
Note: in the subprocess side, fflush(stdout) and fflush(stderr) are called as needed.
没有测试过这个,但你能做到吗?
import sys
pp = subprocess.Popen(command, stderr = fpw, stdout = sys.stdout)
I82Much, thank you for your answer and comment.
Yes, your comment about stderr was absolutely right. I was developing in windows7. In linux (ubuntu10), the following simply worked out:
cmd_line4sub = command + ' 2> ' + ftempname pp = subprocess.Popen(['/usr/bin/xterm','-e',cmd_line4sub],stderr = fpw)
It opens new shells, printing subprocess stdout. The parent process captures subprocess stderr. It is my first goal.
If I could ever figure out how to do in windows, I'll post my answer;)
(I'm the same user as the questioner, but I cannot make them the same account...)
链接地址: http://www.djcxy.com/p/77132.html