#!/usr/bin/python2.6 '''A quick test of what happens to threads with tracebacks''' import sys import time import pprint import random # The "thread" module is deprecated, so we use threading import threading WAIT_TIME = 60 class Test_thread(threading.Thread): ''' A test thread class that sleeps a random number of seconds and intentionally tracebacks, terminating the thread ''' def __init__(self, threadid, name): sys.stderr.write('Test_thread __init__ {0}\n'.format(name)) threading.Thread.__init__(self) self.threadid = threadid self.name = name def run(self): sys.stderr.write('Test_thread run {0}\n'.format(self.name)) # We wait up to 90% of WAIT_TIME before intentionally # raising an exception that generates a traceback. seconds = random.random() * WAIT_TIME * 9.0 / 10.0 time.sleep(seconds) # This of course tracebacks print(1 / 0) def main(): '''Main function''' num_threads = 5 thread_list = [] for threadno in range(num_threads): print('starting thread {0}'.format(threadno)) thread = Test_thread(threadno, 'Thread-{0}'.format(threadno)) thread_list.append(thread) for thread in thread_list: thread.start() time_slice = 5 for dummy in range(int(WAIT_TIME / time_slice)): pprint.pprint(threading.enumerate()) time.sleep(time_slice) # at this point, the main thread is still running, but the other threads have all exited with a traceback print('Done') main()