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?

0 0
Tube Jacker

Tube Jacker

Download Tube Jacker And Discover Everything You Need To Know About Jacking Unlimited Traffic From The Video Giant. The drop dead easy way to create winning video campaigns that will FLOOD your website with unstoppable FREE traffic, all on complete and total autopilot. How to exploit a sneaky method of boosting exposure and getting your videos to the top of Google within 72 hours, guaranteed.

Get My Free Ebook

Post a comment