I need to pass exceptions across a thread boundary.
I'm using python embedded in a non thread safe app which has one thread safe call, post_event(callable), which calls callable from its main thread.
I am running a pygtk gui in a seperate thread, so when a button is clicked I post an event with post_event, and wait for it to finish before continuing. But I need the caller to know if the callee threw an exception, and raise it if so. I'm not too worried about the traceback, just the exception itself.
My code is roughly:
class Callback():def __init__(self,func,*args):self.func=funcself.args=argsself.event=threading.Event()self.result=Noneself.exception=Nonedef __call__(self):gtk.gdk.threads_enter()try:self.result=self.func(*self.args)except:#what do I do here? How do I store the exception?passfinally:gtk.gdk.threads_leave()self.event.set()def post(self):post_event(self)gtk.gdk.threads_leave()self.event.wait()gtk.gdk.threads_enter()if self.exception:raise self.exceptionreturn self.result
Any help appreciated, thanks.