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

D

Doru Moisa

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.
 
K

kyosohma

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.

When talking about anything advanced with wxPython, you usually want
to submit it to the wxPython user's group. Lots of advanced users
(including the author of wxPython) answer questions there.

I would recommend looking at the following link as this will probably
be one of the first suggestions:
http://wiki.wxpython.org/index.cgi/LongRunningTasks?highlight=(task)|(longrunning)

wxPython mailing list: http://www.wxpython.org/maillist.php

I used a variation of one of the threading example in the
LongRunningTasks page to send output to a text widget, through stdout
redirection.

Good luck!

Mike
 
R

Ratko

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top