Fourth Edition Update: As I was updating this chapter in February 2010, Cygwin’s
official Python was still Python 2.5.2. To get Python 3.1 under Cygwin, it had to be
built from its source code. If this is still required when you read this, make sure you
have gcc and make installed on your Cygwin, then fetch Python’s source code package
from python.org, unpack it, and build Python with the usual commands:
./configure
make
make test
sudo make install
This will install Python as python3. The same procedure works on all Unix-like plat-
forms; on OS X and Cygwin, the executable is called python.exe; elsewhere it’s named
python. You can generally skip the last two of the above steps if you’re willing to run
Python 3.1 out of your own build directory. Be sure to also check if Python 3.X is a
standard Cygwin package by the time you read this; when building from source you
may have to tweak a few files (I had to comment-out a #define in Modules/main.c), but
these are too specific and temporal to get into here.
Threads
Threads are another way to start activities running at the same time. In short, they run
a call to a function (or any other type of callable object) in parallel with the rest of the
program. Threads are sometimes called “lightweight processes,” because they run in
parallel like forked processes, but all of them run within the same single process. While
processes are commonly used to start independent programs, threads are commonly
used for tasks such as nonblocking input calls and long-running tasks in a GUI. They
also provide a natural model for algorithms that can be expressed as independently
running tasks. For applications that can benefit from parallel processing, some devel-
opers consider threads to offer a number of advantages:
Performance
Because all threads run within the same process, they don’t generally incur a big
startup cost to copy the process itself. The costs of both copying forked processes
and running threads can vary per platform, but threads are usually considered less
expensive in terms of performance overhead.
Simplicity
To many observers, threads can be noticeably simpler to program, too, especially
when some of the more complex aspects of processes enter the picture (e.g., process
exits, communication schemes, and zombie processes, covered in Chapter 12).
Shared global memory
On a related note, because threads run in a single process, every thread shares the
same global memory space of the process. This provides a natural and easy way
for threads to communicate—by fetching and setting names or objects accessible
to all the threads. To the Python programmer, this means that things like global
186 | Chapter 5: Parallel System Tools