Capturing stdout without waiting for the process end

Luigi luigipaioro at libero.it
Mon Apr 3 11:33:46 EDT 2006


Hi to all!

I'd like to execute an external program capturing the stdout/stderr
messages at "real-time". I mean that I don't want to wait for the end
of the process. If I write a code like this:

import os
import sys

class Runner:

    def run(self, arg):
        try:
            fin, fout = os.popen4(arg)
            self.outData = fout.readlines()
            self.outStatus = fout.close()

        except Exception, err:
            print err

    def printOut(self):
        print "###"
        print self.outStatus
        print "###"
        for line in self.outData:
            print line


r = Runner()
r.run("ls /tmp")
r.printOut()

I can print out (in this case in the os.stdout, but it may be
elsewhere) the whole external program output only once it ends. How can
I do to intercept the external program output during the processing?

Thank you in advance

Luigi




More information about the Python-list mailing list