1 \section{\module{thread
} ---
2 Multiple threads of control.
}
3 \declaremodule{builtin
}{thread
}
5 \modulesynopsis{Create multiple threads of control within one interpreter.
}
8 This module provides low-level primitives for working with multiple
9 threads (a.k.a.\
\dfn{light-weight processes
} or
\dfn{tasks
}) --- multiple
10 threads of control sharing their global data space. For
11 synchronization, simple locks (a.k.a.\
\dfn{mutexes
} or
\dfn{binary
12 semaphores
}) are provided.
13 \index{light-weight processes
}
14 \index{processes, light-weight
}
15 \index{binary semaphores
}
16 \index{semaphores, binary
}
18 The module is optional. It is supported on Windows NT and '
95, SGI
19 IRIX, Solaris
2.x, as well as on systems that have a
\POSIX{} thread
20 (a.k.a. ``pthread'') implementation.
22 \indexii{threads
}{\POSIX{}}
24 It defines the following constant and functions:
26 \begin{excdesc
}{error
}
27 Raised on thread-specific errors.
30 \begin{datadesc
}{LockType
}
31 This is the type of lock objects.
34 \begin{funcdesc
}{start_new_thread
}{function, args
\optional{kwargs
}}
35 Start a new thread. The thread executes the function
\var{function
}
36 with the argument list
\var{args
} (which must be a tuple). The
37 optional
\var{kwargs
} argument specifies a dictionary of keyword
39 function returns, the thread silently exits. When the function
40 terminates with an unhandled exception, a stack trace is printed and
41 then the thread exits (but other threads continue to run).
44 \begin{funcdesc
}{exit
}{}
45 Raise the
\exception{SystemExit
} exception. When not caught, this
46 will cause the thread to exit silently.
49 \begin{funcdesc
}{exit_thread
}{}
50 \deprecated{1.5.2}{Use
\function{exit()
}.
}
51 This is an obsolete synonym for
\function{exit()
}.
54 %\begin{funcdesc}{exit_prog}{status}
55 %Exit all threads and report the value of the integer argument
56 %\var{status} as the exit status of the entire program.
57 %\strong{Caveat:} code in pending \code{finally} clauses, in this thread
58 %or in other threads, is not executed.
61 \begin{funcdesc
}{allocate_lock
}{}
62 Return a new lock object. Methods of locks are described below. The
63 lock is initially unlocked.
66 \begin{funcdesc
}{get_ident
}{}
67 Return the `thread identifier' of the current thread. This is a
68 nonzero integer. Its value has no direct meaning; it is intended as a
69 magic cookie to be used e.g. to index a dictionary of thread-specific
70 data. Thread identifiers may be recycled when a thread exits and
71 another thread is created.
75 Lock objects have the following methods:
77 \begin{methoddesc
}[lock
]{acquire
}{\optional{waitflag
}}
78 Without the optional argument, this method acquires the lock
79 unconditionally, if necessary waiting until it is released by another
80 thread (only one thread at a time can acquire a lock --- that's their
81 reason for existence), and returns
\code{None
}. If the integer
82 \var{waitflag
} argument is present, the action depends on its
83 value: if it is zero, the lock is only acquired if it can be acquired
84 immediately without waiting, while if it is nonzero, the lock is
85 acquired unconditionally as before. If an argument is present, the
86 return value is
\code{1} if the lock is acquired successfully,
90 \begin{methoddesc
}[lock
]{release
}{}
91 Releases the lock. The lock must have been acquired earlier, but not
92 necessarily by the same thread.
95 \begin{methoddesc
}[lock
]{locked
}{}
96 Return the status of the lock:\
\code{1} if it has been acquired by
97 some thread,
\code{0} if not.
104 Threads interact strangely with interrupts: the
105 \exception{KeyboardInterrupt
} exception will be received by an
106 arbitrary thread. (When the
\module{signal
}\refbimodindex{signal
}
107 module is available, interrupts always go to the main thread.)
110 Calling
\function{sys.exit()
} or raising the
\exception{SystemExit
}
111 exception is equivalent to calling
\function{exit_thread()
}.
114 Not all built-in functions that may block waiting for I/O allow other
115 threads to run. (The most popular ones (
\function{time.sleep()
},
116 \method{\var{file
}.read()
},
\function{select.select()
}) work as
120 It is not possible to interrupt the
\method{acquire()
} method on a lock
121 --- the
\exception{KeyboardInterrupt
} exception will happen after the
122 lock has been acquired.
125 When the main thread exits, it is system defined whether the other
126 threads survive. On SGI IRIX using the native thread implementation,
127 they survive. On most other systems, they are killed without
128 executing
\keyword{try
} ...
\keyword{finally
} clauses or executing
130 \indexii{threads
}{IRIX
}
133 When the main thread exits, it does not do any of its usual cleanup
134 (except that
\keyword{try
} ...
\keyword{finally
} clauses are honored),
135 and the standard I/O files are not flushed.