class Mythread(threading.Thread): # subclass Thread object
def init(self, myId, count, mutex):
self.myId = myId
self.count = count # per-thread state information
self.mutex = mutex # shared objects, not globals
threading.Thread.init(self)
def run(self): # run provides thread logic
for i in range(self.count): # still sync stdout access
with self.mutex:
print('[%s] => %s' % (self.myId, i))
stdoutmutex = threading.Lock() # same as thread.allocate_lock()
threads = []
for i in range(10):
thread = Mythread(i, 100, stdoutmutex) # make/start 10 threads
thread.start() # starts run method in a thread
threads.append(thread)
for thread in threads:
thread.join() # wait for thread exits
print('Main thread exiting.')
The output of this script is the same as that shown for its ancestors earlier (again, threads
may be randomly distributed in time, depending on your platform):
C:\...\PP4E\System\Threads> python thread-classes.py
...more deleted...
[4] => 98
[8] => 97
[9] => 97
[5] => 98
[3] => 99
[6] => 98
[7] => 98
[4] => 99
[8] => 98
[9] => 98
[5] => 99
[6] => 99
[7] => 99
[8] => 99
[9] => 99
Main thread exiting.
Using the threading module this way is largely a matter of specializing classes. Threads
in this module are implemented with a Thread object, a Python class which we may
customize per application by providing a run method that defines the thread’s action.
For example, this script subclasses Thread with its own Mythread class; the run method
will be executed by the Thread framework in a new thread when we make a Mythread
and call its start method.
200 | Chapter 5: Parallel System Tools