The threading
module
is built on top of module thread
and supplies
multithreading functionality in a more usable form. The general
approach of threading
is similar to that of Java,
but locks and conditions are modeled as separate objects (in Java,
such functionality is part of every object), and threads cannot be
directly controlled from the outside (meaning there are no
priorities, groups, destruction, or stopping). All methods of objects
supplied by threading
are
atomic.
threading
provides numerous classes for dealing
with threads, including Thread
,
Condition
, Event
,
RLock
, and Semaphore
. Besides
factory functions for the classes detailed in the following sections
of this chapter, threading
supplies the
currentThread
factory
function.
A Thread
object
t
models a thread. You can pass
t
’s main function as an
argument when you create t
, or you can
subclass Thread
and override the
run
method (you may also override __init__
, but should not override other methods).
t
is not ready to run when you create it:
to make t
ready (active), call
t
.start( )
. Once
t
is active, it terminates when its main
function ends, either normally or by propagating an exception. A
Thread
t
can be a
daemon, meaning that Python can terminate even if
t
is still active, while a normal
(non-daemon) thread keeps Python alive until the thread terminates.
Class Thread
exposes the following constructor and
methods.
The
threading ...
No credit card required