[Python-Dev] Interrupting a thread

Christian Tismer tismer@appliedbiometrics.com
Wed, 14 Jul 1999 14:13:10 +0200


Mark Hammond wrote:
...

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

Ah, thanks, that was what I'd like to know :-)

> When the "service thread" recieves a shut-down request from NT, how can it
> interrupt Medusa?

Very simple. I do this shutdown stuff already, at a user request.
Medusa has its polling loop which is so simple (wait until a timeout,
then run again) that I pulled it out of Medusa, and added a polling
function. I have even simulated timer objects by this, which do 
certain tasks from time to time (at the granularity of the loop of
course). One of these looks if there is a global object in module
__main__ with a special name which is executable. This happens to
be the shutdown, which may be injected by another thread as well.
I can send you an example.

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

Well, yes, I would want to have this too, and see also no way.

> [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,

You mean you would catch every signal in the one thread, and
redirect it to the right fake thread.
Given exactly two real threads, one always sitting waiting in a
multiple select, the other running any number of fake threads.
Would this be enough to do everything which is done with threads
today?

maybe-almost-ly y'rs - chris

-- 
Christian Tismer             :^)   <mailto:tismer@appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101   :    *Starship* http://starship.python.net
10553 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home