python - 使用 shell stdout 捕获子进程的 stderr
以下是我想做的事情:
-python process captures stderr of multiple subprocesses to watch the subprocesses -each subprocess runs on the separate window displaying stdout.
当我使用 Popen(command,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)。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
I82Much,谢谢您的回答和评论。
是的,您对 stderr 的评论是绝对正确的。我是在windows7下开发的。在linux(ubuntu10)中,简单地解决了以下问题:
它打开新的shell,打印子进程stdout。父进程捕获子进程的stderr。这是我的第一个目标。
如果我能弄清楚如何在 Windows 中执行操作,我会发布我的答案;)
(我与提问者是同一用户,但我不能使他们成为同一帐户......)
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:
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...)
还没有测试过这个,但是你可以吗
Haven't tested this, but can you do