processes and threading class worker(threading thread)def __init__(selfwork_queuewordnumber)super(__init__(self work_queue work_queue self word word self number number def run(self)while truetryfilename self work_queue get(self process(filenamefinallyself work_queue task_done(the __init__(method must call the base class __init__(the work queue is the same queue queue shared by all the threads we have made the run(method an infinite loop this is common for daemon threadsand makes sense here because we don' know how many files the thread must process at each iteration we call queue queue get(to get the next file to work on this call will block if the queue is emptyand does not have to be protected by lock because queue queue handles that automatically for us once we have file we process itand afterward we must tell the queue that we have done that particular job--calling queue queue task_done(is essential to the correct working of queue queue join(we have not shown the process(functionbecause apart from the def linethe code is the same as the code used in grepword- -child py from the previous "line to the end ( one final point to note is that included with the book' examples is grepwordm pya program that is almost identical to the grepword- py program reviewed herebut which uses the multiprocessing module rather than the threading module the code has just three differencesfirstwe import multiprocessing instead of queue and threadingsecondthe worker class inherits multiprocessing process instead of threading threadand thirdthe work queue is multiprocessing joinablequeue instead of queue queue the multiprocessing module provides thread-like functionality using forking on systems that support it (unix)and child processes on those that don' (windows)so locking mechanisms are not always requiredand the processes will run on whatever processor cores the operating system has available the package provides several ways of passing data between processesincluding using queue that can be used to provide work for processes just like queue queue can be used to provide work for threads