Exceptions in threads
Martin von Loewis
loewis at informatik.hu-berlin.de
Thu Jan 24 14:16:16 EST 2002
Dale Strickland-Clark <dale at riverhall.NOTHANKS.co.uk> writes:
> I was kind of hoping for some undocumented onException() or onEnd()
> methods that I could override. A bit of a long shot, I grant you.
How would that work? A thread is called "thread" because it is a
single thread of control; it always does its thing, and the outside
world cannot force it to do anything else.
If you have thread objects, nothing stops you from doing
class NotiableThread(threading.Thread):
def onException(self):
print "onexception called"
def start_new_thread(self, *args):
t = NotifyingThread(self, *args):
r.start()
return t
def NotifyingThread(threading.Thread):
def __init__(self, parent, *args):
self.parent = parent
threading.Thread.__init__(self, *args)
def run(self):
try:
threading.Thread.run(self)
except:
self.parent.onException()
With this architecture, a thread that terminates with an exception
will invoke a onException operation on the parent thread. However,
this invocation will occur in the content of the *child* thread, not
in the context of the parent thread. Having the parent thread execute
something else just makes no sense.
Regards,
Martin
More information about the Python-list
mailing list