multithread exception handling
steve.horsley at gmail.com
Mon Sep 19 21:46:42 CEST 2005
pegazik at gmail.com wrote:
> I have problem and I ask you for help. Probably there is some quite
> easy solution, but I can't see it.
> I'm trying to perform some action that have to be timeout safe. So here
> is the structure of my program:
> def TimeoutHandler():
> print '!'
> raise Exception
> class Active:
> def Action:
> timer = Timer(1, TimeoutHandler)
> print '1'
> print '2'
> print '3'
> print '4'
> print '5'
> print '6'
> active = Actice()
> The output is:
> My question is, why exception is not raised correctly? Could be the
> reason that (probably) timer is another thread and there is no
> exception in the main thread? How to solve this problem? [My timeout
> should be smaller than one second so I can't use signal.alarm()]
> Thanks for your reply,
> Krzysztof Nowak
As far as I can see, the exception is raised in the CORRECT
thread - the one (belonging to the Timer) that is executing a
If you want to raise an exception in a different thread, you have
to arrange some inter-thread communication. Except that the way
to communicate between threads is to share an object where one
thread writes to an object's state and another waits for or
periodically checks for a change in the objects state.
It's difficult to say more without knowing what problem you are
trying to solve.
More information about the Python-list