Home > Articles > Open Source > Python

  • Print
  • + Share This
This chapter is from the book

4.11. Exercises


Processes versus Threads. What are the differences between processes and threads?


Python Threads. Which type of multithreaded application will tend to fare better in Python, I/O-bound or CPU-bound?


Threads. Do you think anything significant happens if you have multiple threads on a multiple CPU system? How do you think multiple threads run on these systems?


Threads and Files.

  1. Create a function that obtains a byte value and a filename (as parameters or user input) and displays the number of times that byte appears in the file.
  2. Suppose now that the input file is extremely large. Multiple readers in a file is acceptable, so modify your solution to create multiple threads that count in different parts of the file such that each thread is responsible for a certain part of the file. Collate the data from each thread and provide the correct total. Use the timeit module to time both the single- threaded new multithreaded solutions and say something about the difference in performance, if any.


Threads, Files, and Regular Expressions. You have a very large mailbox file—if you don’t have one, put all of your e-mail messages together into a single text file. Your job is to take the regular expressions you designed earlier in this book that recognize e-mail addresses and Web site URLs and use them to convert all e-mail addresses and URLs in this large file into live links so that when the new file is saved as an .html (or .htm) file, it will show up in a Web browser as live and clickable. Use threads to segregate the conversion process across the large text file and collate the results into a single new .html file. Test the results on your Web browser to ensure the links are indeed working.


Threads and Networking. Your solution to the chat service application in the previous chapter required you to use heavyweight threads or processes as part of your solution. Convert your solution to be multithreaded.


*Threads and Web Programming. The Crawler application in Chapter 10, “Web Programming: CGI and WSGI,” is a single-threaded application that downloads Web pages. It would benefit from MT programming. Update crawl.py (you could call it mtcrawl.py) such that independent threads are used to download pages. Be sure to use some kind of locking mechanism to prevent conflicting access to the links queue.


Thread Pools. Instead of a producer thread and a consumer thread, change the code for prodcons.py, in Example 4-12 so that you have any number of consumer threads (a thread pool) which can process or consume more than one item from the Queue at any given moment.


Files. Create a set of threads to count how many lines there are in a set of (presumably large) text files. You can choose the number of threads to use. Compare the performance against a single-threaded version of this code. Hint: Review the exercises at the end of the Chapter 9, in Core Python Programming or Core Python Language Fundamentals.


Concurrent Processing. Take your solution to Exercise 4-9 and adopt it to a task of your selection, for example, processing a set of e-mail messages, downloading Web pages, processing RSS or Atom feeds, enhancing message processing as part of a chat server, solving a puzzle, etc.


Synchronization Primitives. Investigate each of the synchronization primitives in the threading module. Describe what they do, what they might be useful for, and create working code examples for each.

The next couple of exercises deal with the candy.py script featured in Example 4-11.


Porting to Python 3. Take the candy.py script and run the 2to3 tool on it to create a Python 3 version called candy3.py.


The threading module. Add debugging to the script. Specifically, for applications that use semaphores (whose initial value is going to be greater than 1), you might need to know exactly the counter’s value at any given time. Create a variation of candy.py, perhaps calling it candydebug.py, and give it the ability to display the counter’s value. You will need to look at the threading.py source code, as alluded to earlier in the CORE TIP sidebar. Once you’re done with the modifications, you can alter its output to look something like the following:

$ python candydebug.py
starting at: Mon Apr  4 00:24:28 2011
THE CANDY MACHINE (full with 5 bars)!
Buying candy... inventory: 4
Refilling candy... inventory: 5
Refilling candy... full, skipping
Buying candy... inventory: 4
Buying candy... inventory: 3
Refilling candy... inventory: 4
Buying candy... inventory: 3
Buying candy... inventory: 2
Buying candy... inventory: 1
Buying candy... inventory: 0
Buying candy... empty, skipping
all DONE at: Mon Apr  4 00:24:36 2011
  • + Share This
  • 🔖 Save To Your Account