Thread lag

A

Asterix

Hi,

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

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

Here is the way I call the class:

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

def _on_message_decrypted(output, val):
# blabla

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


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

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

Thanks for your help
 
P

Piet van Oostrum

Asterix said:
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.
 
Y

Yann Leboulanger

Piet said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top