[pygtk] idle_add questions

Doug Quale quale1 at charter.net
Fri Aug 20 11:34:39 WST 2004


Steve McClure <smcclure at racemi.com> writes:

> > My basic problem is this, I have a dialog that takes ages to load data. 
> > I thought I would do the data retrieval and dialog update in an idle
> > function.  However, my initial dialog never gets displayed making me
> > think that I'm not getting back to the event loop before the idle task
> > runs.  That really doesn't seem to be the case though since the GUI
> > responds to user input, albeit, incredibly slow. So slow as to really be
> > unusable.
> 
> It seems to be working fine, now that I found the real priority numbers.
> If I can just figure out why everything comes to a screeching halt as
> soon as I return gtk.FALSE from my idle function.

I hope someone else can give you more definitive answer, but I wanted
to point out something about idle functions in gtk+.  Idle functions
aren't designed for long running computations unless they can be
broken up into small, quick pieces.  I think you will want to
cooperatively handle gui events during your long computation or use a
separate thread.

See the pygtk FAQ item 3.7 for a discussion of this.
http://www.async.com.br/faq/pygtk/index.py?req=show&file=faq03.007.htp

Idle functions are scheduled when gtk+ doesn't have any other work to
perform, but gtk+ doesn't preempt them.  Each idle function will run
to completion once it's started.  Using a high priority actually makes
things worse rather than better since gtk+ will then run the slow
computation instead of handling gui events and the gui will freeze.
(The gtk+ docs say "Note that you will interfere with GTK+ if you use
a priority above GTK_PRIORITY_RESIZE.")

You can process gui events in your idle function using
gtk.main_iteration() if you can run this every so often

    while gtk.events_pending():
        gtk.main_iteration()

If there's no way to break up your computation to run the main loop
then you should consider using a separate thread.  [Another
possibility that avoids threads is to use an event driven framework
like twisted.]


More information about the pygtk mailing list