Raising exceptions in threads
Saturday, June 06, 2009
It's not simply raising an exception in a thread that I want. I want to raise an exception in a thread from another thread. It's like sending signals to threads, only signals in pyhon can only be delivered to the main thread (for portability). But Python has a other asynchronous signaling system: exceptions.
I'd like to be able to do something like:
for t in threading.enumerate():
thread.raise(MyAppDoesntWantYouAnymoreError)
Is this possible? Are there other ways to do this sort of thing?
Alternatively I might be happy with a fix for issue1856, but I do think it might be nice to be able to signal threads in an asynchronous way in any case.
3 comments:
Doug Hellmann said...
This might do what you want, but I'm not sure how portable it is: http://sebulba.wikispaces.com/recipe+thread2
Unknown said...
That is exaclty what I was looking for! Just slightly odd that this is only exposed on the C API, but I guess it's fine using c-types.
Anonymous said...
Btw, the recipe has moved to http://tomerfiliba.com/recipes/Thread2/
New comments are not allowed.