Passing Exceptions Across Threads

robert no-spam at no-spam-no-spam.com
Wed Apr 26 08:01:28 EDT 2006


Adam Mullins wrote:

> Hello, I'm writing a physics simulator back-end with a built-in,
> threaded server that for the moment is quite simple. I've faced a few
> problems in writing this code, however, as it's the first time I've
> played with threading. For the moment, everything works decently, but I
> need (or rather, want) a method for passing caught exceptions in
> sub-threads to the main thread to be raised there. Although this
> solution isn't the only one, I feel it will be the most elegant (the
> thread/class will be called inside the main module within a try/except
> structure, which makes the program a bit more modular, in my opinion,
> and cleaner).
> 
> Here is my code so far:
> http://rafb.net/paste/results/UESOWB24.html
> 
> You can see near the top where I clumsily tried to a hack a function
> into threading._MainThread, in the hopes that if it were called from a
> sub-thread it would execute in the main thread. This is seemingly not
> so.
> 
> Many thanks for any help.
> 

The CallQueue and BackgroundCall.get_return() do that exception transfer 
already by default:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/491281
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/491280

-robert



More information about the Python-list mailing list