471,318 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,318 software developers and data experts.

exception from a thread

assume a scenario, where there is a class that for doing some work,
spawns lot of threads.

class X:
def __init__(self):
# Spawns some threads to
# do some work

now, assuming that at least one of the threads encounters a problem
while doing work and throws an exception. Would I be able to handle
that error in the caller class? When I try to do this, I see that the
errors thrown by the threads never cascade to this caller :(

Jun 28 '07 #1
1 1172
_spitFIRE schrieb:
assume a scenario, where there is a class that for doing some work,
spawns lot of threads.

class X:
def __init__(self):
# Spawns some threads to
# do some work

now, assuming that at least one of the threads encounters a problem
while doing work and throws an exception. Would I be able to handle
that error in the caller class? When I try to do this, I see that the
errors thrown by the threads never cascade to this caller :(
Exactly. Because they are different threads - the very essence of them
is each of them having their own stacks.

Depending on what you do in the main-thread, you can do something like this:
class Worker(Thread):
def __init__(self):
...
self.running = False
...
self.start()

def run(self):
self.running = True
try:
... # work

finally:
self.running = False
In the main-thread, you test for each thread if it's still running.

while True:

time.sleep(.1)
for t in threads:
if not t.running:
print "Not running anymor: %r" % t

Diez
Jun 28 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by dcrespo | last post: by
4 posts views Thread by Bhavya Shah | last post: by
5 posts views Thread by David | last post: by
44 posts views Thread by craig | last post: by
5 posts views Thread by Lucvdv | last post: by
16 posts views Thread by Chuck Cobb | last post: by
2 posts views Thread by Robinson | last post: by
7 posts views Thread by Brendon Costa | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.