pathnames [ for in path('listdir(if isfile()paths [pathnames[ ::cpusfor in range(cpus)query_queues [queue(for in range(cpus)results_queue queue(search_procs process(target=searchargs=(pqresults_queue)for pq in zip(pathsquery_queuesfor proc in search_procsproc start(for easier descriptionlet' assume cpu_count is four notice how the import statements are placed inside the if guardthis is small optimization that prevents them from being imported in each subprocess (where they aren' neededon certain operating systems we list all the paths in the current directory and then split the list into four approximately equal parts we also construct list of four queue objects to send data into each subprocess finallywe construct single results queuethis is passed into all four of the subprocesses each of them can put data into the queue and it will be aggregated in the main process now let' look at the code that makes search actually happenfor in query_queuesq put("def" put(nonesignal process termination for in range(cpus)for match in results_queue get()print(matchfor proc in search_procsproc join(this code performs single search for "def(because it' common phrase in directory full of python files!in more production ready systemwe would probably hook socket up to this search code in that casewe' have to change the inter-process protocol so that the message coming back on the return queue contained enough information to identify which of many queries the results were attached to this use of queues is actually local version of what could become distributed system imagine if the searches were being sent out to multiple computers and then recombined we won' discuss it herebut the multiprocessing module includes manager class that can take lot of the boilerplate out of the preceding code there is even version of the multiprocessing manager that can manage subprocesses on remote systems to construct rudimentary distributed application check the python multiprocessing documentation if you are interested in pursuing this further