polling for output from a subprocess module



Hello,
My program uses the subprocess module to spawn a child and capture its
output. What I'd like to achieve is that stdout is parsed after the
subprocess finishes, but anything that goes to stderr is printed
immediately. The code currently looks like:

try:
test = Popen(test_path,
stdout=PIPE,
stderr=PIPE,
close_fds=True,
env=test_environ)

while test.poll() == None:
ready = select.select([test.stderr], [], [])

if test.stderr in ready[0]:
t_stderr_new = test.stderr.readlines()
if t_stderr_new != []:
print "STDERR:", "\n".join(t_stderr_new)
t_stderr.extend(t_stderr_new)

except OSError, e:
print >>sys.stderr, _("Test execution failed"), e
else:
self.result.return_code = test.returncode
self.result.process(test.stdout.readlines(), t_stderr)


The problem is, that it seems that all the output from the subprocess
seems to be coming at once. Do I need to take a different approach?
.



Relevant Pages

  • Re: Pascal code checker!
    ... You can use subprocess to run the pascal program and capture the ... output/results. ...
    (comp.lang.python)
  • Re: Problem with Parallel::ForkManager on Windows Vista
    ... I should have mentioned that each forked child runs a system command ... It would appear that if the children don't capture any stdout the ... problem I goes away -- the system commands terminate as do the child ...
    (comp.lang.perl.misc)
  • Re: Chaining programs with pipe
    ... you do the same thing you would in C. Create a pipe ... then run one program with stdout connected to ... The doc page for subprocess doesn't say what platforms support ... As is typical for Windows, ...
    (comp.lang.python)
  • Re: Dialog with a process via subprocess.Popen blocks forever
    ... from the subprocess' stdout stream (better: file descriptor)) reading ... from the subprocess stdout blocks forever. ... buffer when reading from two pipes. ...
    (comp.lang.python)
  • detached subprocess
    ... After struggling with os.spawnxxx to get a detached process I tried using Pyhton2.4's new subprocess module. ... print>>sys.stderr, 'stderr IN THE CHILD' ...
    (comp.lang.python)