views:

450

answers:

2

Say I write this:

from subprocessing import Popen, STDOUT, PIPE
p = Popen(["myproc"], stderr=STDOUT, stdout=PIPE)

Now if I do

line = p.stdout.readline()

my program waits until the subprocess outputs the next line.

Is there any magic I can do to p.stdout so that I could read the output if it's there, but just continue otherwise? I'm looking for something like Queue.get_nowait()

I know I can just create a thread for reading p.stdout, but let's assume I can't create new threads.

+3  A: 

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()
Nadia Alramli
+3  A: 

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.

Alex Martelli
+1 I like this solution
Nadia Alramli
What exactly is returned? This still doesn't give a clue how much bytes is there to read.
iElectric
`select` returns the set of file descriptors (among those you passed it) on which I/O can be performed without blocking. If you can arrange for p.stdout to be set to non-blocking after the fact (system dependent) you can just p.stdout.read(N) for some large N and get all bytes that are in fact available; if the nonblocking option is not available, all you can do is read 1 byte at a time and go back to `select`ing until that tells you there are no more. Be sure to append the bytes you read into a list and ''.join it at the end (much faster than +='ng away on strings!-).
Alex Martelli