开发者

Python subprocess.Popen stdin interfering with stdout/stderr

Very puzzled by this. When using Popen, if only using stdout or stderr the following code works:

def run(self):
    self.externalProcess = subprocess.Popen(['./external_process.out 1>&2'], shell=True, stderr=subprocess.PIPE)
    while self.externalBinary.poll() is None:
        print('Still running')
    print('Done running')

I'm using the stderr file descriptor because I'm monitoring the process output real time in a GUI and stderr is unbuffered. See my other question for more background on that mess: Python capture stdout from subprocess line by line

The problem I'm having here is that as soon as I add stdi开发者_如何学Cn to allow user input to be passed to the external process it starts acting as though stdout is buffered again:

def run(self):
    self.externalProcess = subprocess.Popen(['./external_process.out 1>&2'], shell=True, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
    while self.externalBinary.poll() is None:
        print('Still running')
    print('Done running')

My question is why does stdin appear to be affecting stdout and stderr?


It seems like you might be trying to write data to the subprocess (via STDIN) as well as read its output (via STDOUT) interactively.

As mentioned in the answer to this SO question, that is a no-no!

You could use Popen.communicate, but that is a blocking call which will wait for all communication to finish (you can't poll as you do in your example)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜