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