Say I write this:
from subprocessing import Popen, STDOUT, PIPE
p = Popen([\"myproc\"], stderr=STDOUT, stdout=PIPE)
Now if I do
Use the select
module in Python's standard library, see http://docs.python.org/library/select.html . select.select([p.stdout.fileno()], [], [], 0)
immediately returns a tuple whose items are three lists: the first one is going to be non-empty if there's something to read on that file descriptor.
Use p.stdout.read(1)
this will read character by character
And here is a full example:
import subprocess
import sys
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
while True:
out = process.stdout.read(1)
if out == '' and process.poll() != None:
break
if out != '':
sys.stdout.write(out)
sys.stdout.flush()