Hi,

Am Mon, den 05.07.2004 um 14:38 Uhr -0400 schrieb John Russell:
> I have read a few threads on this mailing list where file IO was
> discussed.  Specifically this thread
> 
> http://www.daa.com.au/pipermail/pygtk/2003-November/006233.html
> 
> which talks about running something like
> 
>               while 1:
>                       data_o = child_o.readline()
>                       textbuffer.insert(textbuffer.get_end_iter(), data_o)
>                       textview.scroll_to_mark(textbuffer.get_insert(), 0)
>                       while gtk.events_pending():
>                               gtk.mainiteration(gtk.FALSE)
> 
> to read a line, update the UI and read some more.
> 
> However, I am reading from the stdout of a file descriptor returned
> from popen and the process that I am running sometimes thinks for a
> while.  So if its thinking and doesn't output anything, the UI will
> freeze while it blocks on the readline() call.  When a line shows up,
> it is appended, the UI updates all of a sudden, and then back to
> blocking where the UI doesn't update.

Switch the stuff to non blocking mode :)

> How can I make this more smooth?  I was thinking of threads, but I
> couldn't figure out how to stop the process if, for example, the user
> hit cancel or something.  I tried join(0) but the process just sat
> there, even after my program had exited.  I had to kill -9 the spawned
> process.  I'm sure there is a way to do this as I see it all the time.
>  e.g. the output of cdrecord in xcdroast... that kind of thing.  How
> is this done?

I use non-blocking reads, no threads, but if you want, have a look at 
http://cvs.sourceforge.net/viewcvs.py/*checkout*/traveller/traveller/spawner.py?rev=1.9

That's how I did it...

As that is under LGPL, you can just use that (spawn() returns the pid,
the object has a onGetData event you connect to using
spawner.onGetData.append((self, getdata_cb)) and will get the pid and
group and data)
or you can write your own :)

The important points are:
open stuff with Popen2.Popen4, manipulate the returned "fromchild" by
fcntl to set flag O_NONBLOCK, use gtk.input_add to make it callback on
read and except.

In that callback do the gui update. If read() function throws exception,
it means nothing bad; if read() function returns "" that means "End of
file", and that means, the command (the pipe endpoint) has been
terminated.

Hope that helps.

cheers,
   Danny

Attachment: signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil

_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to