Modify the Page Indexer application so that it uses multiple secondary threads instead of just one. By getting the number of secondary threads right, the application could be made to run faster than the single secondary thread version. Although this exercise involves writing or modifying only about 100 lines of code, it is quite subtle and challenging.
The approach taken in the solution is to move os.walk() to the primary thread, and create a list of filenames. Whenever the list has 1 000 files, a secondary thread is created to process those files. At the end, another secondary thread is created to process whatever files remain. The Walker.initialize() method is not required since we can pass all the parameters to the constructor. And changes to Walker.run() and Walker.processFiles() are quite small. Most of the changes must be made in the pageindexer.pyw file.
The setPath() method is where the filenames can be gathered and secondary threads created to process them. In the solution we used a separate method to create the secondary threads. Since there could be many secondary threads we also added a stopWalkers() method and modified the finished(), accept(), reject(), and finishedIndexing() methods. Since some of the widgets in the user interface could potentially be accessed in response to signals from more than one thread, we protect accesses to them with a mutex.
Make sure that the threads are deleted when they are no longer needed to avoid creating more and more threads each time setPath() is called.
The new version of the walker module should be a bit shorter than the original, but the new page indexer will be about 90 lines longer than the original. A solution is provided in chap19/pageindexer_ans.pyw and chap19/walker_ans.py.
Was this article helpful?
Video Marketing Will Never Be The Same Again. Here's How YOU - Or ANYONE - Can Leverage On The Internet's 4th MOST TRAFFICKED WEBSITE TO BOOST YOUR POPULARITY amp PUBLICITY FOR BUSINESS via Video Marketing! With The Emergence Of Web Video Popularity, You CANNOT Afford To Miss Out On This Exponentially Growing Trend - Because It Could Make Or Break Your Popularity amp Business Growth!