14.1 Threads in Python
Python
offers multithreading on platforms that support threads, such as
Win32, Linux, and most variants of Unix. The Python interpreter does
not freely switch threads. Python uses a global interpreter lock
(GIL) to ensure that switching between threads happens only between
bytecode instructions or when C code deliberately releases the GIL
(Python's C code releases the GIL around blocking
I/O and sleep operations). An action is said to be
atomic if it's guaranteed that
no thread switching within Python's process occurs
between the start and the end of the action. In practice, an
operation that looks atomic actually is atomic when executed on an
object of a built-in type (augmented assignment on an immutable
object, however, is not atomic). However, in general it is not a good
idea to rely on atomicity. For example, you never know when you might
be dealing with a derived class rather than an object of a built-in
type, meaning there might be callbacks to Python code.
Python offers multithreading in two different flavors. An older and
lower-level module, thread, offers a bare minimum
of functionality, and is not recommended for direct use by your code.
The higher-level module threading, built on top of
thread, was loosely inspired by
Java's threads, and is the recommended tool. The key
design issue in multithreading systems is most often how best to
coordinate multiple threads. threading therefore
supplies several synchronization objects. Module
Queue is very useful for thread synchronization as
it supplies a synchronized FIFO queue type, which is extremely handy
for communication and coordination between threads.
|