Added a test for the ability to specify a class attribute in Formatter configuration...
[python.git] / Doc / lib / libthread.tex
blob4914948d6fb9a485fb2ff7952fdd6a25f1a37d86
1 \section{\module{thread} ---
2 Multiple threads of control}
4 \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, Linux, SGI
19 IRIX, Solaris 2.x, as well as on systems that have a \POSIX{} thread
20 (a.k.a. ``pthread'') implementation. For systems lacking the \module{thread}
21 module, the \refmodule[dummythread]{dummy_thread} module is available.
22 It duplicates this module's interface and can be
23 used as a drop-in replacement.
24 \index{pthreads}
25 \indexii{threads}{\POSIX}
27 It defines the following constant and functions:
29 \begin{excdesc}{error}
30 Raised on thread-specific errors.
31 \end{excdesc}
33 \begin{datadesc}{LockType}
34 This is the type of lock objects.
35 \end{datadesc}
37 \begin{funcdesc}{start_new_thread}{function, args\optional{, kwargs}}
38 Start a new thread and return its identifier. The thread executes the function
39 \var{function} with the argument list \var{args} (which must be a tuple). The
40 optional \var{kwargs} argument specifies a dictionary of keyword arguments.
41 When the function returns, the thread silently exits. When the function
42 terminates with an unhandled exception, a stack trace is printed and
43 then the thread exits (but other threads continue to run).
44 \end{funcdesc}
46 \begin{funcdesc}{interrupt_main}{}
47 Raise a KeyboardInterrupt in the main thread. A subthread can use this
48 function to interrupt the main thread.
49 \versionadded{2.3}
50 \end{funcdesc}
52 \begin{funcdesc}{exit}{}
53 Raise the \exception{SystemExit} exception. When not caught, this
54 will cause the thread to exit silently.
55 \end{funcdesc}
57 %\begin{funcdesc}{exit_prog}{status}
58 %Exit all threads and report the value of the integer argument
59 %\var{status} as the exit status of the entire program.
60 %\strong{Caveat:} code in pending \keyword{finally} clauses, in this thread
61 %or in other threads, is not executed.
62 %\end{funcdesc}
64 \begin{funcdesc}{allocate_lock}{}
65 Return a new lock object. Methods of locks are described below. The
66 lock is initially unlocked.
67 \end{funcdesc}
69 \begin{funcdesc}{get_ident}{}
70 Return the `thread identifier' of the current thread. This is a
71 nonzero integer. Its value has no direct meaning; it is intended as a
72 magic cookie to be used e.g. to index a dictionary of thread-specific
73 data. Thread identifiers may be recycled when a thread exits and
74 another thread is created.
75 \end{funcdesc}
78 Lock objects have the following methods:
80 \begin{methoddesc}[lock]{acquire}{\optional{waitflag}}
81 Without the optional argument, this method acquires the lock
82 unconditionally, if necessary waiting until it is released by another
83 thread (only one thread at a time can acquire a lock --- that's their
84 reason for existence). If the integer
85 \var{waitflag} argument is present, the action depends on its
86 value: if it is zero, the lock is only acquired if it can be acquired
87 immediately without waiting, while if it is nonzero, the lock is
88 acquired unconditionally as before. The
89 return value is \code{True} if the lock is acquired successfully,
90 \code{False} if not.
91 \end{methoddesc}
93 \begin{methoddesc}[lock]{release}{}
94 Releases the lock. The lock must have been acquired earlier, but not
95 necessarily by the same thread.
96 \end{methoddesc}
98 \begin{methoddesc}[lock]{locked}{}
99 Return the status of the lock:\ \code{True} if it has been acquired by
100 some thread, \code{False} if not.
101 \end{methoddesc}
103 \strong{Caveats:}
105 \begin{itemize}
106 \item
107 Threads interact strangely with interrupts: the
108 \exception{KeyboardInterrupt} exception will be received by an
109 arbitrary thread. (When the \refmodule{signal}\refbimodindex{signal}
110 module is available, interrupts always go to the main thread.)
112 \item
113 Calling \function{sys.exit()} or raising the \exception{SystemExit}
114 exception is equivalent to calling \function{exit()}.
116 \item
117 Not all built-in functions that may block waiting for I/O allow other
118 threads to run. (The most popular ones (\function{time.sleep()},
119 \method{\var{file}.read()}, \function{select.select()}) work as
120 expected.)
122 \item
123 It is not possible to interrupt the \method{acquire()} method on a lock
124 --- the \exception{KeyboardInterrupt} exception will happen after the
125 lock has been acquired.
127 \item
128 When the main thread exits, it is system defined whether the other
129 threads survive. On SGI IRIX using the native thread implementation,
130 they survive. On most other systems, they are killed without
131 executing \keyword{try} ... \keyword{finally} clauses or executing
132 object destructors.
133 \indexii{threads}{IRIX}
135 \item
136 When the main thread exits, it does not do any of its usual cleanup
137 (except that \keyword{try} ... \keyword{finally} clauses are honored),
138 and the standard I/O files are not flushed.
140 \end{itemize}