python - 使用 shell stdout 捕获子进程的 stderr

发布于 2024-10-25 18:43:03 字数 1687 浏览 1 评论 0原文

以下是我想做的事情:

-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平定天下 2024-11-01 18:43:04

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

是的,您对 stderr 的评论是绝对正确的。我是在windows7下开发的。在linux(ubuntu10)中,简单地解决了以下问题:

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

它打开新的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:

    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...)

杯别 2024-11-01 18:43:03

还没有测试过这个,但是你可以吗

import sys

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

Haven't tested this, but can you do

import sys

pp = subprocess.Popen(command, stderr = fpw, stdout = sys.stdout)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文