polling for output from a subprocess module

jakub.hrozek at gmail.com jakub.hrozek at gmail.com
Mon Feb 4 07:29:40 EST 2008


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?



More information about the Python-list mailing list