Python Threading

From 118wiki

Jump to: navigation, search

Python Threading

Python supplies two modules for concurrent and parallel processing, thread and threading. The threading module is built upon thread and supplies some nice functionality. We've already seen how to use threading in a homework, but in that case each thread was an independent process, not needing to share data with any other thread. What if we want different threads to share data? We could, of course, use sockets, TCP, and messages to share data, but that seems wasteful since the threads are part of the same program running on the same host. What about just using a global variable (could be a list, dictionary, or any kind of object) and sharing data with that?

Here's what can happen: (demonstration of media:Concur.txt explained in class). This demonstration shows how the unpredictable way threads execute could lead to trouble in sharing access to a variable. Python provides several techniques to manage the sharing of data, including locks, the Queue module, condition objects, event objects, and semaphore objects. We'll mainly look at the Lock object. Here's how it can be used to get correct execution of the previous example: (demonstration of media:ConcurL.txt).

Personal tools