I'm using subprocess to launch, well, sub-processes, but now I'm
stumbling due to blocking I/O.
Is there a way for me to know that there's data on a pipe, and possibly
how much data is there so I can get it? Currently I'm doing this:
process = subprocess.Pope n(
args,
bufsize=1,
universal_newli nes=True,
stdout=subproce ss.PIPE,
stderr=subproce ss.PIPE)
def ProcessOutput(i nstream, outstream):
text = instream.readli ne()
if len(text) 0:
print >>outstream, text,
return True
else:
return False
while process.poll() is None:
ProcessOutput(p rocess.stdout, sys.stdout)
ProcessOutput(p rocess.stderr, sys.stderr)
# clean up everything to EOF once the process ends.
somethingPrinte d = True
while somethingPrinte d:
somethingPrinte d = ProcessOutput(
process.stdout, sys.stdout)
somethingPrinte d |= ProcessOutput(
process.stderr, sys.stderr)
Unfortunately, stream.readline will block 'til it gets a line, and
typically there won't be anything on the stderr stream. The reason for
the redirections in the first place is that I'm launching this script as
a subprocess from a GUI app that catches stdout and stderr and directs
the output to the appropriate windows, but in some cases I don't
actually want the output at all (I've removed that logic though since it
needlessly complicates my example; suffice to say everything below the
process = subprocess.Pope n... line is enclosed in a try and then in an
if block.
The documentation on file.read() indicate that there's an option for
"non-blocking" mode, but I'm stumped as to how to even look for how to
enable and use that.
thanks,
-tom!
--
stumbling due to blocking I/O.
Is there a way for me to know that there's data on a pipe, and possibly
how much data is there so I can get it? Currently I'm doing this:
process = subprocess.Pope n(
args,
bufsize=1,
universal_newli nes=True,
stdout=subproce ss.PIPE,
stderr=subproce ss.PIPE)
def ProcessOutput(i nstream, outstream):
text = instream.readli ne()
if len(text) 0:
print >>outstream, text,
return True
else:
return False
while process.poll() is None:
ProcessOutput(p rocess.stdout, sys.stdout)
ProcessOutput(p rocess.stderr, sys.stderr)
# clean up everything to EOF once the process ends.
somethingPrinte d = True
while somethingPrinte d:
somethingPrinte d = ProcessOutput(
process.stdout, sys.stdout)
somethingPrinte d |= ProcessOutput(
process.stderr, sys.stderr)
Unfortunately, stream.readline will block 'til it gets a line, and
typically there won't be anything on the stderr stream. The reason for
the redirections in the first place is that I'm launching this script as
a subprocess from a GUI app that catches stdout and stderr and directs
the output to the appropriate windows, but in some cases I don't
actually want the output at all (I've removed that logic though since it
needlessly complicates my example; suffice to say everything below the
process = subprocess.Pope n... line is enclosed in a try and then in an
if block.
The documentation on file.read() indicate that there's an option for
"non-blocking" mode, but I'm stumped as to how to even look for how to
enable and use that.
thanks,
-tom!
--
Comment