Passing Exceptions Across Threads

A

Adam Mullins

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.
 
A

aurelien.campeas

have you tried replacing :

main = threading._MainThread()

with

main = threading.currentThread() ?

(not sure if that will be sufficient)

Well your way to pass exception between threads looks like I would have
done myself. But I am no expert.

Have you considered using stackless Python ? It provides a much safer
and efficient threading model (thinking about your physics simulator).
Of course, if you want not to block on blocking calls (socket stuff),
real threads are a way to go.
 
R

robert

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

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top