PyGTK blocks threads without a GUI

I want to play with streaming errors with PyGTK. I have this code:

#!/usr/bin/python import pygtk pygtk.require('2.0') import gtk import threading from time import sleep class SomeNonGUIThread(threading.Thread): def __init__(self, tid): super(SomeNonGUIThread, self).__init__() self.tid = tid def run(self): while True: print "Client #%d" % self.tid sleep(0.5) class App(threading.Thread): def __init__(self): super(App, self).__init__() self.window = gtk.Window() self.window.set_size_request(300, 300) self.window.set_position(gtk.WIN_POS_CENTER) self.window.connect('destroy', gtk.main_quit) self.window.show_all() def run(self): print "Main start" gtk.main() print "Main end" if __name__ == "__main__": app = App() threads = [] for i in range(5): t = SomeNonGUIThread(i) threads.append(t) # Ready, set, go! for t in threads: t.start() # Threads work so well so far sleep(3) # And now, they freeze :-( app.start() 

It makes NonGUIThreads work for 3 seconds. The window is subsequently shown, and the remaining threads are stopped ! After closing the window, the threads start again.

How is it possible that gtk.main() is able to block other threads? Threads are independent of any lock, so they should work while the window is displayed.

+7
source share
1 answer

You need to call gobject.threads_init() before doing anything related to Gtk.

Further information in the PyGtk FAQ

+5
source

All Articles