Signals between threads

Warren Postma embed at geocities.com
Mon Feb 12 10:03:07 EST 2001


I am limited by my embedded platform to a single process, with multiple
threads.

I have implemented a time.sleep() modification that makes it interruptable
(I use WaitForSingleObject( hInterruptSleepEvent,ms ) != WAIT_TIMEOUT) in
timemodule.c.

Now I'm considering some improvements to the thread-by-thread signalling
code in my personal python 1.5.2 variant:

import thread,time

def myfunc():
    print "starting"
    try:
        time.sleep(999999)
   except:
       print "time.sleep blew up. must have been sent an exception"
    print "stopping"

thrd = thread.start_new_thread( myfunc, () ) # currently thrd would be None.
time.sleep(1.0)
thrd.raise_exception(KeyboardInterrupt) # the wishfull-thinking bit.
time.sleep(1.0)


This is very much a practical effort to get real work done, but also in the
long term I'm intereseted in contributing threading improvements to the 2.xx
codebase.

In particular, the ability of one thread to send a signal (raise an
exception in another thread), and for the built-in thread module to return a
an object that can be used for this purpose.  (Currently
thread.start_new_thread returns None. I consider that to be Horrid
Behaviour, and the module threading.py to be a nice-API, but which should be
implemented in Python itself.)

I agree with posters who said that interruptable and "killable" threads are
a bad, or dangerous, idea.    I think the ability to inject an exception
into another thread could easily be abused, however I also think it needs to
be there, for when it's essential to getting a job done.

Is anyone else in a similar bind? What are you considering?

Warren Postma






More information about the Python-list mailing list