popen and a long running process in a wx.python application

Ratko rjagodic at gmail.com
Tue Jun 26 15:58:01 EDT 2007


On Jun 26, 10:16 am, Doru Moisa <moisad... at gmail.com> wrote:
> Hello,
>
> How can I capture the output of a long runnning process which I open
> with popen() ?
> I tried reading line by line, char by char, but the result always
> comes when the process finishes.
> (I am trying to make a wx.python program that opens some "make ..."
> with popen). How can I receive the output of the program immediatly,
> so that I can show a progressbar  in my application ?
> I always get the program's output after it finished executing.
> Is this the right place, or should I post this to wx.python ?
>
> Thank you in advance.



I think this is the right list to post this in since it's independent
of wxPython.
I just recently went through this and got it working properly. Your
problem is that the output is buffered and is only flushed upon exit
(and that's when you read() it)
Here's a piece of code:

doRead = True
p = subprocess.Popen(["ls"], stdout=sp.PIPE, stderr=sp.STDOUT,
bufsize=1)
while doRead:
     txt = os.read(p.stdout.fileno(), 2048)
     time.sleep(0.5)   # read at most x times / sec

A few things to note:
(1) you must use os.read and not the builtin read() for unbuffered
read.
(2) you probably want to run this while loop in a thread because read
will block until there's some text available
(3) this is a polling method where I read at most 2 a second in the
above example
(4) this works on Mac, Windows and Linux the same (besides the "ls"
command of course)
(5) after you read this text you can send it to a TextCtrl or
something

Hope that helps.

Ratko




More information about the Python-list mailing list