0

I have a program which opens a subprocess and communicates with it by writing to its stdin and reading from its stdout.

proc = subprocess.Popen(['foo'],
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        stdin=subprocess.PIPE)

proc.stdin.write('stuff\n')
proc.stdin.flush()

The problem is that when reading, it always blocks if I call proc.stdout.read(), and when I try to read line by line using the following:

output = str()
while proc.stdout in select.select([proc.stdout], [], [])[0]:
    output += proc.stdout.readline()

it still blocks because select.select returns proc.stdout even after all the output has been read already. What can I do?

note that I am not using proc.communicate because I would like to communicate with the process multiple times

b_pcakes
  • 2,452
  • 3
  • 28
  • 45
  • What kind of output are you expected to read from `stdout`? A fixed number of lines? A fixed number of bytes? Multiple lines until an empty line is found? Other? In other words: when should is program supposed to stop reading from stdout and start doing something else? – Andrea Corbellini Mar 16 '16 at 18:13
  • ... or maybe do you want to read only what's available right now? (With the intent of implementing what is known as "asynchronous communication") – Andrea Corbellini Mar 16 '16 at 18:15
  • How much data is the subprocess producing to stdout? There's a [warning about PIPEs filling up](https://docs.python.org/3/library/subprocess.html#subprocess.Popen.stderr) and blocking subprocesses. – Ilja Everilä Mar 16 '16 at 18:18
  • it is not a fixed number of lines; i write a command to the process and it returns some number of lines, which varies. – b_pcakes Mar 16 '16 at 18:19
  • http://stackoverflow.com/questions/12419198/python-subprocess-readlines-hangs seems pretty similar – Ilja Everilä Mar 16 '16 at 18:22

0 Answers0