简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM