def __init__(selfmyidcountmutex)self myid myid self count count self mutex mutex threading thread __init__(selfsubclass thread object per-thread state information shared objectsnot globals def run(self)run provides thread logic for in range(self count)still sync stdout access with self mutexprint('[% =% (self myidi)stdoutmutex threading lock(threads [for in range( )thread mythread( stdoutmutexthread start(threads append(threadfor thread in threadsthread join(print('main thread exiting 'same as thread allocate_lock(make/start threads starts run method in thread wait for thread exits the output of this script is the same as that shown for its ancestors earlier (againthreads may be randomly distributed in timedepending on your platform) :\pp \system\threadspython thread-classes py more deleted [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = main thread exiting using the threading module this way is largely matter of specializing classes threads in this module are implemented with thread objecta python class which we may customize per application by providing run method that defines the thread' action for examplethis script subclasses thread with its own mythread classthe run method will be executed by the thread framework in new thread when we make mythread and call its start method parallel system tools