Home > Articles > Programming > Python

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

4.11. Exercises

4-1.

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

4-2.

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

4-3.

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?

4-4.

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.

4-5.

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.

4-6.

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.

4-7.

*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.

4-8.

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.

4-9.

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.

4-10.

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.

4-11.

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.

4-12.

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.

4-13.

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