Reliable Non Blocking Reads From Subprocess Stdout
Note: I have a process that writes one line to stdout ('print('hello')) and waits for raw_input. I run this process with p = subprocess.Popen, then call p.stdout.readline()....thi
Solution 1:
readline
reads a single line from the file-like object PIPE
, to read it all of it, simply wrap it in a while loop. You should also call sleep
after each read to save on CPU cycles.
Here is a simple example:
import subprocess
p = subprocess.Popen(
['ls', '-lat'],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE
)
while True:
line = p.stdout.readline()
if line == '':
breakprint(line.strip()) # remove extra ws between lines
EDIT:
woah, sorry, I completely missed the part you were trying to read input in that other process...
So, fn your other process, looks something like:
print('Hello')
in = raw_input()
Then the print actually sends the content to the file-like object PIPE
you passed earlier which has it's own buffering mechanism. This behavior is explained in the print()
function docs
To solve this simply add a sys.stdout.flush()
between your print
and raw_input
:
print('Hello')
sys.stdout.flush() # "flush" the output to our PIPE
in = raw_input()
Post a Comment for "Reliable Non Blocking Reads From Subprocess Stdout"