Thread lag

Yann Leboulanger yann.le_boulanger at u-paris10.fr
Sat Apr 25 18:06:36 EDT 2009


Piet van Oostrum wrote:
>>>>>> Asterix <asterix at lagaule.org> (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.

Hehe, great! That was it!
Thank you
-- 
Yann



More information about the Python-list mailing list