How to get a raised exception from other thread

Lasse Vågsæther Karlsen lasse at vkarlsen.no
Sun Oct 16 14:31:52 EDT 2005


Peter Hansen wrote:
> themightydoyle at gmail.com wrote:
> 
>> Nevermind.  I found a better solution.  I used shared memory to create
>> a keep-alive flag.  I then use the select function with a specified
>> timeout, and recheck the keep-alive flag after each timeout.
> 
> 
> As Dennis points out, your original attempt was destined to fail because 
> you were calling the method from the main thread, not the one you wanted 
<snip>

A similar problem exists in .NET but they "solved" it by creating an 
Abort method that raises an exception in the thread itself, basically 
doing what you wanted to do.

However, this leads to all sorts of problem, particular when that 
exception is raised in a finally block in that thread, which could mean 
that the finally block did not fully execute, leading to resource leaks 
or similar problems.

They pondered a while about constructing non-interruptible blocks of 
code for .NET 2.0 but I'm not entirely sure how that worked out.

In any case, the best way is to use some kind of signal that the thread 
reacts to, either by having it specifically wait for a signallable 
object (like an event) or just by checking a boolean variable or similar 
for a magic value that means "now is a good time for you to terminate".

-- 
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:lasse at vkarlsen.no
PGP KeyID: 0x2A42A1C2



More information about the Python-list mailing list