multithread exception handling

Piet van Oostrum piet at
Tue Sep 20 15:07:47 CEST 2005

>>>>> pegazik at (p) wrote:

[Timer example snipped]
>p> My question is, why exception is not raised correctly? Could be the
>p> reason that (probably) timer is another thread and there is no
>p> exception in the main thread? 

yes, the doc of Timer says it is a subclass of Thread, and therefore runs in
a new thread.
By the way, your program isn't even correct Python, so how can it give the
output you have written down?

When I run a similar (but corrct python) program, I do get an exception,
but it is in another thread, and therefore not caught.
You can post a signal to the main thread, however, to catch the exception:

def TimeoutHandler():
    print '!'
    os.kill(0, signal.SIGUSR1)

def handler():
    raise Exception

class Active:
    def __init__(self):
        signal.signal(signal.SIGUSR1, handler)
        timer = Timer(1, TimeoutHandler)
Piet van Oostrum <piet at>
URL: [PGP 8DAE142BE17999C4]
Private email: piet at

More information about the Python-list mailing list