GNOME Bugzilla – Bug 461551
gtk.main() hangs if gtk.gdk.threads_init() was called
Last modified: 2010-12-23 21:13:23 UTC
pygtk thread support on windows in effectively broken. pygtk applications hang after gtk.gdk.threads_init() is called on Python 2.5.1 Threads created between gtk.gdk.threads_init() call and gtk.main() call are working and reporting ok, but GUI hangs - only borders are visible, no window background is painted, no response to keyboard/mouse events. An example workaround described at http://faq.pygtk.org/index.py?req=show&file=faq21.003.htp doesn't work.
Created attachment 92654 [details] Broken GTK threading support on win32
You need to call threads_init() before everything else; normally you'd just do import gobject gobject.threads_init() import gtk ... does that work?
With gobject it works, but not with gtk.gdk import gtk.gdk gtk.gdk.threads_init() ...
You need to call both, it's well known: http://faq.pygtk.org/index.py?req=show&file=faq20.001.htp
The FAQ entry doesn't say about the need to call both. If I call gtk.gdk.threads_init() immediately after gobject.threads_init(), the application hangs again. import gobject gobject.threads_init() import gtk.gdk gtk.gdk.threads_init() ... The FAQ entry talks about deprecated gtk.threads_init(), but uses gobject.threads_init() in examples. It also doesn't mention that threads_init() should be called even before other imports. Testcase works ok when I just replace gtk.gdk.threads_init() with gobject.threads_init()
See examples in examples/threading/
On Windows you only ever want to use gobject.threads_init() and you should never even need to touch gtk.gdk.threads_init(). gtk.gdk.threads_init() initializes the GDK global lock that serializes thread access to the GTK and GDK libraries. As far as I understand, this means all of the sudden multiple threads can start modifying the GUI. Unlike X11, on Windows only a single thread is allowed to change the GUI (usually the main thread is used for this). The example mentioned above lives here: http://git.gnome.org/browse/pygtk/tree/examples/threading/gtk_threading.py