#!/usr/bin/python """Repro case; trying to distill this "bug" down to the essentials.""" import thread import threading import time # These values may be OS- and System-dependent # These values are correct on my Windows XP box #NUM_THREADS_OK = 1000 #NUM_THREADS_FOR_FAIL = 1100 # These values are correct on my OS X box. NUM_THREADS_OK = 2400 NUM_THREADS_FOR_FAIL = 2600 def do_work(): """Let's just wait 10 seconds.""" time.sleep(10) def create_threads(num_threads): for index in range(num_threads): athread = threading.Thread(target=do_work) try: athread.start() except thread.error: print('Failed to create a thread') print('A thread will be stuck in "initial" state because of this.') while threading.activeCount() > 1: # Uncomment following line to see the types ("initial," "started," # etc.) of threads. #print('Thread list: %s' % threading.enumerate()) print('There are %s active threads; waiting for them to end' % threading.activeCount()) time.sleep(2) print('Everything ended successfully. The world is a happy place.') if __name__ == "__main__": create_threads(NUM_THREADS_OK) create_threads(NUM_THREADS_FOR_FAIL)