Wednesday, September 01, 2010

Python PyGtk + Multi-thread

I chose Python language to make UI development easier. With Glade, a buggy but still functionally okay utility, I can design UI in a short time and load it up with my Python program.

I am doing some socket operations in this program, but found that multi-threading in this program seems to be malfunctioning. And the socket accept action never returns and continue and running my code. After some hours research, I found that GTK will lockout all other threads if I do not perform proper init.

So here is the trick when using PyGtk + multi-thread:

1. import gtk
2. gtk.gdk.threads_init()

With these two lines of code, the thread works happily. Note that multi-threaded applications need to add UI related actions to GTK main loop instead of touching them directly.