[Tutor] Getting process output in real time

Jeff Shannon jeff@ccvcorp.com
Thu May 1 13:51:01 2003


Neil Hodge wrote:

> All:
>
> I am building a GUI to wrap around a CLI program.  One of the 
> functions of the CLI outputs a progress bar, something like the 
> following:
>
> [0....25....50....75....100]
>
> I am using one of the os.popen variants for all of my other system 
> calls, and getting the stdout works perfectly.  Since all of the other 
> functions are very quick or give no status updates, this would be a 
> moot question.  But for this function, I would like to get the stdout 
> back in real time so that I can display it in the GUI. 


You may be able to use read() on your pipe object to, in essence, poll 
for new data.  Perhaps easiest would be to fire off your CLI program in 
a secondary thread, and use read(1) to grab individual bytes of output 
from it and then post those back to your main (GUI) thread.  This lets 
your GUI remain responsive while your long-running console task is 
processing, and gives you a fair amount of flexibility in how you 
present the progress reports (you could simply update a text widget, or 
run a graphical progressbar dialog, or whatever).

Jeff Shannon
Technician/Programmer
Credit International