[pygtk] Asynchronous access at output of os.popen() call

togix togix at libero.it
Wed Aug 20 17:16:39 WST 2008


Hi,
I'm working on a GUI in PyGTK that allows to insert some parameters and then to call a program (written in C) using the os.popen() function.

The thing that I want to do is redirect the standard output of the called program into a gtk.TextView() box [b]in runtime[/b], so to show the lines when they come out in a asynchronous way, not all the end of execution.


eg. imagine that I want to call a simple program "work.exe" that prints out some line, waiting some seconds in between: 

[code]
/* Program work.c */
int main (int argc, char *argv[])
{
      int i = 0;
      for (i=0; i<4; i++)
      {
            printf("Working... %d\n", i);
            sleep(5);
      }
      printf("END");
      return(0);
}
[/code]

And then call it from the Python interface:

[code]
...
textview = gtk.TextView()
textbuffer = textview.get_buffer()

cmd = "./work.exe"
for line in os.popen(cmd):
      textbuffer.insert_at_cursor(line)
      while gtk.events_pending():
            gtk.main_iteration(False)
...
[/code]

The problem is that I obtain all this output in a "block", at the end of the execution of the program, not one line every 5 second as I'd like to:

[code]
Working... 0
Working... 1
Working... 2
Working... 3
END
[/code]

How can I do to?
Any suggestions will be appreciated.




More information about the pygtk mailing list