[pygtk] idle_add vs. threads_enter/threads_leave

Tim Evans t.evans at aranz.com
Wed Jul 7 09:44:35 WST 2010

On 2010-07-07 13:26, Jason Heeris wrote:
> Tim Evans wrote:
>> GTK+ 2.14.4
>> PyGObject 2.14.2
>> PyGTK 2.12.1
> Mine is
> GTK+ 2.20
> PyGObject 2.21.2
> PyGTK 2.17.1
> A few things about your changes confused me -
> 1. You call glib.idle_add, but never called glib.threads_init - won't
> this break on Linux?
> 2. You use BOTH gtk.gdk.lock and glib.idle_add - but surely one of
> these is superfluous, since glib.idle_add executes the function in the
> main loop, where the lock is already acquired?
> Anyway, I came up with a different, simpler way, but I don't know
> enough to say if it will never fail. I can't push to github from here,
> but basically it's the same as my first example[1] but with this main
> block for controller.py:

My older version of PyGObject doesn't have glib.threads_init(), or even 
a separate glib module, that was one of the new features. With the older 
version you didn't have the option of turning on threads in pygobject 
without turning on threads in gdk. Perhaps you still don't if you want 
things to work, I'm not sure.

Idle and timeout callbacks are called without the gdk thread lock. You 
need to acquire it in each callback. In practise I always use a custom 
idle_add_lock that does that for me. Again, this may be specific to my 
older PyGObject.

   def idle_add_lock(func, *args):
       def idle():
           with gtk.gdk.lock:
               return func(*args)
       return gobject.idle_add(idle)

Tim Evans
Applied Research Associates NZ

More information about the pygtk mailing list