Thread lag

Piet van Oostrum piet at
Sat Apr 25 22:19:51 CEST 2009

>>>>> Asterix <asterix at> (A) a écrit:

>A> Hi,
>A> Here is a small class I use in my GTK application, to do some job in a
>A> thread.:

>A> class ThreadInterface:
>A>    def __init__(self, func, func_args, callback, callback_args):
>A>       '''Call a function in a thread and then call callback'''
>A>       def thread_function(func, func_args, callback, callback_args):
>A>          print 'func start'
>A>          output = func(*func_args)
>A>          gobject.idle_add(callback, output, *callback_args)
>A>       Thread(target=thread_function, args=(func, func_args, callback,
>A>          callback_args)).start()
>A>       print 'thread called'

>A> Here is the way I call the class:

>A> def decrypt_thread(encmsg, keyID):
>A>    # Do things
>A>    return val1, val2)

>A> def _on_message_decrypted(output, val):
>A>    # blabla

>A> ThreadInterface(decrypt_thread, [encmsg, keyID], _on_message_decrypted,
>A> [val])

>A> But between the time "thread called" is printed and the time "func
>A> start" is printed, there is sometimes (it vary a lot) several seconds.

>A> How could it be? Why thread is not started instantly? How can I improve
>A> that?

I don't know if it is related to our problem but I guess it is.

It appears that GTK and Python threads are incompatible UNLESS you call
gtk.gdk.threads_init() before gtk.main(). This has something to do with
releasing the GIL, which gtk optimizes away if it hasn't been told that
your application uses threads.
Piet van Oostrum <piet at>
URL: [PGP 8DAE142BE17999C4]
Private email: piet at

More information about the Python-list mailing list