[pygtk] PyGTK Thread problem

Funky Fred funkyfredmale at hotmail.com
Sun Apr 15 12:39:01 WST 2007



Just to throw the idea out there:

You can use a hybrid between idle_add & threads.
I built  a gui with threads that build a data structure, and invoke 
callbacks on an update queue thread that mostly sleeps, but occasionally 
wakes up and checks for pending updates. If there are updates pending, it 
invokes a callback on the main (gui) class. Since I occasionally got weird 
lock-ups having the update thread invoke a callback that calls gtk methods 
in another thread (I think because it doesn't actually get executed in that 
other thread), I just have the update thread invoke a callback that updates 
a flag in the gui (gtk) thread, and use a gtk idle func (via idle_add) that 
checks the update flag, and updates the gui as necessary.

A bit intricate, I guess, but it works, no lock-ups, and the gui updates 
nicely.

The only real catch to pygtk, threads, and python, is that pythons thread 
management is .. lacking. There is no real way to signal threads to 
shutdown. So if someone closes the gui, the best you can do is invoke a 
callback on the other thread that updates a flag, they hopefully check that 
in time to shutdown before the gui closes and/or you exit python. This can 
be problematic if your background threads do computationally intense stuff 
and don't necessarily check their "die" flag in time.

Just a few thoughts...

-Stu

>From: Thomas Güttler <hv at tbz-pariv.de>
>To: pygtk at daa.com.au
>Subject: Re: [pygtk] PyGTK Thread problem
>Date: Fri, 13 Apr 2007 08:51:10 +0200
>
>Am Donnerstag, 12. April 2007 18:32 schrieb Rodrigo Uroz:
> > Regarding this, another problem. Now I get my thread to update the GUI
> > through a callback in my main thread, but the TextView gets updated only
> > the first time, then the callback gets called again and again but 
>doesn't
> > automatically update the GUI (I have to click in the TextView to update
> > it).
> >
> > What could be going wrong?, I couldn't find a solution looking in the 
>mail
> > list.
> >
>
>Is your main thread busy? Updates only happen after the main thread
>returns from the callback.
>
>BTW: I switched from threads to idle_add. It is much easier to debug.
>
>http://www.async.com.br/faq/pygtk/index.py?req=show&file=faq03.007.htp
>
>"""
>3.7. While my callback is executing, nothing is refreshed in the 
>application
>windows!
>If you have a long-running callback, or one that modifies the application
>windows during its execution, you will notice that the windows of your app
>freeze for the duration of the callback. This is by design: all gtk events
>(including window refreshing and updates) are handled in the mainloop, and
>while it has branched to process your callback, it can't handle window 
>update
>events.Therefore nothing will happen in the application windows.
>
>The trick here is to realize where your callback can take a while to 
>return,
>or where it is dynamically changing the window contents, and add a section
>like this in the body of your callback handler:
>
>  while gtk.events_pending():
>    gtk.main_iteration(False
>"""
>
>--
>Thomas Güttler, http://www.tbz-pariv.de/
>Bernsdorfer Str. 210-212, 09126 Chemnitz, Tel.: 0371/5347-917
>_______________________________________________
>pygtk mailing list   pygtk at daa.com.au
>http://www.daa.com.au/mailman/listinfo/pygtk
>Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

_________________________________________________________________
Get a FREE Web site, company branded e-mail and more from Microsoft Office 
Live! http://clk.atdmt.com/MRT/go/mcrssaub0050001411mrt/direct/01/



More information about the pygtk mailing list