Thread interruption

David Allen s2mdalle at titan.vcu.edu
Wed Jan 24 00:20:55 EST 2001


I'm using the threading module, and creating thread
objects by passing a reference to a function as part
of the object creation (rather than using an object
and overriding run)

My question is, how do I interrupt threads?  In each
thread, there will be blocking system calls happening,
and I need to be able to interrupt the threads very
quickly.  This is not something where select() is
the answer, because I'm worried about blocking
from gethostbyname() and other socket related stuff
too.

Optimally, I would be able to raise an exception in
a thread other than the one I was currently in,
something along the lines of 

someThreadObject.raiseException(KeyboardInterrupt)

or something like that.  But I can't find any 
mechanism to do that.

I've seen the Event objects in the documentation,
but it seems like the thread acknowledging the event
would have to check for it and do something.  In
other words, it looked like you couldn't just 
pass some event object to another thread and have
that thread drop whatever it was doing and deal 
with that.  Can events actually do this?  If so,
I'd appreciate a pointer to some more documentaiton
or a code sample of this in action.

Otherwise, how can I do this?  If I was writing in
C, I might be using multiple processes instead of
threads and maybe I'd kill the process I wanted to
wake up with SIGINT or something like that.  But
from what I've read about threads, if I send a signal,
I can't be assured that any particular thread will
get it, it's just "some arbitrary thread".

On a completely unrelated note, I thought python
threading was done within one process, and that
the threading code had to deal with "timeslicing"
between all the threads that actually belonged to
one process.  But checking the output of 'ps' on
running a threaded python program, actually 4-5
instances of the interpreter seem to be running when
I'm using threads.  What gives?

-- 
David Allen
http://opop.nols.com/
----------------------------------------
Common sense is the collection of prejudices acquired by age eighteen. 
- Albert Einstein



More information about the Python-list mailing list