[pygtk] pixbuf loading in a thread -- release the GIL!

evan battaglia gtoevan at gmx.net
Wed Dec 12 15:31:17 WST 2007


Hi,
I'm trying to write a program which uses threads to load large images and scale them down to a medium size (maybe 640x480) in the background. The idea is to keep maybe 5 images at a time loaded and show the next one quickly without having to wait.

The problem is that in my thread, during the call to gtk.gdk.pixbuf_new_from_file() and in gtk.gdk.Pixbuf.scale_simple() the UI is blocked. So I downloaded the pygtk source, edit gtk/gdk.c, and changed the code to release and reacquire the GIL (for example, in _wrap_gdk_pixbuf_new_from_file):

    pyg_begin_allow_threads;
    ret = gdk_pixbuf_new_from_file(filename, &error);
    pyg_end_allow_threads;

I ran my script again and it worked perfectly! So my questions are:

 * is there a reason why this is not in pygtk now? Do we really need to keep the GIL? pixbuf_new_from_file only reads from a file and produces no data, so I would think other threads couldn't harm it. (Unless, of course, they modify the filename, but that could be strdup'd). Would it be OK for scale_simple too, or could another thread modify the pixbuf?

 * is there a better way to accomplish what I am trying to do -- load large images in the background without freezing the UI? It seems like this must be a common problem (don't programs have to load thumbnails in the background?!)

Anyway, if you need me to provide a diff (presumably for gtk/gdk.override) and/or an example I'd be happy to.

Thanks,
Evan




-- 
GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail


More information about the pygtk mailing list