On Feb 19, 2011, at 12:39 PM, Robert Schroll wrote:
> Hi all,
>
> I have a multi-threaded GTK application. There is a mainloop thread
> than handles all the GUI stuff and one or more worker threads for
> computation. These worker threads can post callbacks with
> gobject.idle_add if they want changes to the GUI.
>
> What I'd like to do is have the worker thread post the callback, idle
> until the callback has completed, and then pick back up its execution.
> Is this possible? If so, how? Bonus points if the callback can pass
> information back to the worker thread.
This is what we do:
class IdleBlockCallback:
def __init__(self, func, args=(), kwargs={}):
self.func = func
self.args = args
self.kwargs = kwargs
self.event = threading.Event()
self.result = None
def __call__(self):
gtk.gdk.threads_enter()
try:
self.result = self.func(*self.args, **self.kwargs)
finally:
gtk.gdk.flush()
gtk.gdk.threads_leave()
self.event.set()
return False # don't repeat
def runBlock(func, args=(), kwargs={}):
callbackobj = OOFIdleBlockCallback(func, args, kwargs)
callbackobj.event.clear()
gobject.idle_add(callbackobj, priority=gobject.PRIORITY_LOW)
callbackobj.event.wait()
return callbackobj.result
Call runBlock on the worker thread. Be sure that you're really on the worker
thread, because if you call it on the main thread it will hang.
-- Steve
--
-- [email protected] Tel: (301) 975-5423 --
-- http://math.nist.gov/mcsd/Staff/SLanger/ Fax: (301) 975-3553 --
-- NIST, 100 Bureau Drive, Stop 8910, Gaithersburg, Md 20899-8910 --
-- "I don't think this will work. That's why it's science." --
-- Naomi Langer (age 6), 17 Feb 2003 --
_______________________________________________
pygtk mailing list [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/