[Python-Dev] Interrupting a thread

Mark Hammond MHammond@skippinet.com.au
Wed, 14 Jul 1999 12:25:50 +1000


Ive struck this a number of times, and the simple question is "can we make
it possible to interrupt a thread without the thread's knowledge" or
otherwise stated "how can we asynchronously raise an exception in another
thread?"

The specific issue is that quite often, I find it necessary to interrupt
one thread from another.  One example is Pythonwin - rather than use the
debugger hooks as IDLE does, I use a secondary thread.  But how can I use
that thread to interrupt the code executing in the first? (With magic that
only works sometimes is how :-)

Another example came up on the newsgroup recently - discussion about making
Medusa a true Windows NT Service.  A trivial solution would be to have a
"service thread", that simply runs Medusa's loop in a seperate thread.
When the "service thread" recieves a shut-down request from NT, how can it
interrupt Medusa?

I probably should not have started with a Medusa example - it may have a
solution.  Pretend I said "any arbitary script written to run similarly to
a Unix daemon".  There are one or 2 other cases where I have wanted to
execute existing code that assumes it runs stand-alone, and can really only
be stopped with a KeyboardInterrupt.  I can't see a decent way to do this.

[I guess this ties into the "signals and threads" limitations - I believe
you cant direct signals at threads either?]

Is it desirable?  Unfortunately, I can see that it might be hard :-(

But-sounds-pretty-easy-under-those-fake-threads<wink>-ly,

Mark.