156.html ( File view )

  • By 2010-08-21
  • View(s):12
  • Download(s):0
  • Point(s): 1

Safari | Python Developer's Handbook -> Threads

< BACKMake Note | BookmarkCONTINUE >


Let's start by quickly defining a thread. Many people still have some kind of confusion when it comes to clarifying the difference between threads and processes.

When you run any program in your computer, the CPU creates a process for that program. This process is defined as a group of elements that compound a single program. These elements are the memory area reserved for the program, a program counter, a list of files opened by the program, and a call stack where all the variables are stored. A program with a single call stack and program counter is a single threaded program.

Now, suppose you have different tasks inside your program that you need to execute several times simultaneously. What do you do? Maybe you are thinking about calling the whole program several times. Wrong answer! Think about all the resources that you are consuming without actually using them!

The solution to implement this multithreaded program is to create a function that implements the code which needs to be executed several times concurrently, and then, create a thread that uses only this function.

A thread is a program unit that processes multiple time-consuming actions as parallel tasks in the background of your main application process. Sometimes threads are difficult to debug because the circumstances in which they occur are hard to simulate.

Python Threads

Python threads can be implemented on every operational system that supports the POSIX threads library. But actually, the Python threading support doesn't always use POSIX threads. In the python-2.0 source tree, there are beos, cthread, lwp, nt, os2, pth, pthread, sgi, solaris, and wince thread implementations. In certain environments that support multithreading, Python allows the interpreter to run many threads at once.

Python has two threading interfaces: The thread module and the threading module. The use of these Python's native threading built-in modules enables the code to be portable across all platforms that support Python.

The thread module supports lightweight process threads. It offers a low-level interface for working with multiple threads.

On the other hand, the threading module provides high-level threading interfaces on top of the thread module.

Besides these two modules, Python also implements the Queue module. This is a synchronized queue class used in thread programming to move Python objects between multiple threads in a safe way.

Threads have limitations on some platforms. For instance, Linux thread switching is quite fast, sometimes faster than NT thread switching.

Programssuch as Tkinter, CORBA, and ILUthat rely on a main loop to dispatch events can complicate the design of threads. Definitively, they do not have a good relationship with threaded programs. Main loops are usually used by Graphical User Interfaces not to allow the main thread to exit.

MacPython is currently not built with thread support. That is because no posix-compatible thread implementation was available, making Python integration hard. However, this has changed with GUSI2 (a posix I/O emulation library), and the upcoming MacPython 1.6a1 is planned to have threads.

The Windows Operation System adds many additional features to Python's implementation of threads. The win32 package provides as additional features for Python's thread support:

  • The win32process moduleAn interface to the win32 Process and Thread API's.

  • The win32event moduleA module that provides an interface to the win32 event/wait API.

The threading model provided by the COM technology allows objects not designed to work as threads to be used by other objects that are thread-aware.

Python's interpreter cannot handle more than one thread at the same time. The global interpreter lock is the internal mechanism which guarantees that the Python interpreter executes only one thread simultaneously. Although this is not a problem for single-threaded programs, or programs on single-processor machines, it can become trouble on performance-critical applications that run on multiprocessor computers. If your threads are doing IO work, other threads can execute during reads and writes.

Check out Appendix A, "Python/C API," for information about handling threads using the Python/C API. You can also see the latest documentation about it at


You might also want to look at the thread and threading modules in the library reference, which are documented at




Anton Ertl has a Web page that exposes very interesting material about the differences between the various threading techniques:


Python Thread Modules

Python includes two threading modules, assuming that your Python was configured for threads when it was built. One provides the primitives, and the other provides higher-level access. In general, Python relies on operating system threads unless you specifically compile it by activating the thread directive. This should offer adequate performance for all but the most demanding applications.

Thread Module

The following four functions are available in this module:

Sponsored links
  • Sent successfully!
  • 1 point

156.html (1.65 MB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point


Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com


Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D