471,355 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,355 software developers and data experts.

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

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.

Jun 26 '07 #1
3 1978
On Jun 26, 10:16 am, Doru Moisa <moisad...@gmail.comwrote:
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/L...longrunning%29

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

Jun 26 '07 #2
On Jun 26, 10:16 am, Doru Moisa <moisad...@gmail.comwrote:
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

Jun 26 '07 #3

Mike, Ratko,

Thanks a lot guys, for the quick and prompt answers.

Doru
Jun 27 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Maarten van Veen | last post: by
2 posts views Thread by Greg Ercolano | last post: by
1 post views Thread by walterbyrd | last post: by
4 posts views Thread by commander_coder | last post: by
4 posts views Thread by zane.selvans | last post: by
25 posts views Thread by Jeremy Banks | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.