source: vendor/python/2.5/Doc/lib/libthread.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 6.4 KB
Line 
1\section{\module{thread} ---
2 Multiple threads of control}
3
4\declaremodule{builtin}{thread}
5\modulesynopsis{Create multiple threads of control within one interpreter.}
6
7
8This module provides low-level primitives for working with multiple
9threads (a.k.a.\ \dfn{light-weight processes} or \dfn{tasks}) --- multiple
10threads of control sharing their global data space. For
11synchronization, simple locks (a.k.a.\ \dfn{mutexes} or \dfn{binary
12semaphores}) are provided.
13\index{light-weight processes}
14\index{processes, light-weight}
15\index{binary semaphores}
16\index{semaphores, binary}
17
18The module is optional. It is supported on Windows, Linux, SGI
19IRIX, 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}
21module, the \refmodule[dummythread]{dummy_thread} module is available.
22It duplicates this module's interface and can be
23used as a drop-in replacement.
24\index{pthreads}
25\indexii{threads}{\POSIX}
26
27It defines the following constant and functions:
28
29\begin{excdesc}{error}
30Raised on thread-specific errors.
31\end{excdesc}
32
33\begin{datadesc}{LockType}
34This is the type of lock objects.
35\end{datadesc}
36
37\begin{funcdesc}{start_new_thread}{function, args\optional{, kwargs}}
38Start 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
40optional \var{kwargs} argument specifies a dictionary of keyword arguments.
41When the function returns, the thread silently exits. When the function
42terminates with an unhandled exception, a stack trace is printed and
43then the thread exits (but other threads continue to run).
44\end{funcdesc}
45
46\begin{funcdesc}{interrupt_main}{}
47Raise a \exception{KeyboardInterrupt} exception in the main thread. A subthread
48can use this function to interrupt the main thread.
49\versionadded{2.3}
50\end{funcdesc}
51
52\begin{funcdesc}{exit}{}
53Raise the \exception{SystemExit} exception. When not caught, this
54will cause the thread to exit silently.
55\end{funcdesc}
56
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}
63
64\begin{funcdesc}{allocate_lock}{}
65Return a new lock object. Methods of locks are described below. The
66lock is initially unlocked.
67\end{funcdesc}
68
69\begin{funcdesc}{get_ident}{}
70Return the `thread identifier' of the current thread. This is a
71nonzero integer. Its value has no direct meaning; it is intended as a
72magic cookie to be used e.g. to index a dictionary of thread-specific
73data. Thread identifiers may be recycled when a thread exits and
74another thread is created.
75\end{funcdesc}
76
77\begin{funcdesc}{stack_size}{\optional{size}}
78Return the thread stack size used when creating new threads. The
79optional \var{size} argument specifies the stack size to be used for
80subsequently created threads, and must be 0 (use platform or
81configured default) or a positive integer value of at least 32,768 (32kB).
82If changing the thread stack size is unsupported, a \exception{ThreadError}
83is raised. If the specified stack size is invalid, a \exception{ValueError}
84is raised and the stack size is unmodified. 32kB is currently the minimum
85supported stack size value to guarantee sufficient stack space for the
86interpreter itself. Note that some platforms may have particular
87restrictions on values for the stack size, such as requiring a minimum
88stack size > 32kB or requiring allocation in multiples of the system
89memory page size - platform documentation should be referred to for
90more information (4kB pages are common; using multiples of 4096 for
91the stack size is the suggested approach in the absence of more
92specific information).
93Availability: Windows, systems with \POSIX{} threads.
94\versionadded{2.5}
95\end{funcdesc}
96
97
98Lock objects have the following methods:
99
100\begin{methoddesc}[lock]{acquire}{\optional{waitflag}}
101Without the optional argument, this method acquires the lock
102unconditionally, if necessary waiting until it is released by another
103thread (only one thread at a time can acquire a lock --- that's their
104reason for existence). If the integer
105\var{waitflag} argument is present, the action depends on its
106value: if it is zero, the lock is only acquired if it can be acquired
107immediately without waiting, while if it is nonzero, the lock is
108acquired unconditionally as before. The
109return value is \code{True} if the lock is acquired successfully,
110\code{False} if not.
111\end{methoddesc}
112
113\begin{methoddesc}[lock]{release}{}
114Releases the lock. The lock must have been acquired earlier, but not
115necessarily by the same thread.
116\end{methoddesc}
117
118\begin{methoddesc}[lock]{locked}{}
119Return the status of the lock:\ \code{True} if it has been acquired by
120some thread, \code{False} if not.
121\end{methoddesc}
122
123In addition to these methods, lock objects can also be used via the
124\keyword{with} statement, e.g.:
125
126\begin{verbatim}
127from __future__ import with_statement
128import thread
129
130a_lock = thread.allocate_lock()
131
132with a_lock:
133 print "a_lock is locked while this executes"
134\end{verbatim}
135
136\strong{Caveats:}
137
138\begin{itemize}
139\item
140Threads interact strangely with interrupts: the
141\exception{KeyboardInterrupt} exception will be received by an
142arbitrary thread. (When the \refmodule{signal}\refbimodindex{signal}
143module is available, interrupts always go to the main thread.)
144
145\item
146Calling \function{sys.exit()} or raising the \exception{SystemExit}
147exception is equivalent to calling \function{exit()}.
148
149\item
150Not all built-in functions that may block waiting for I/O allow other
151threads to run. (The most popular ones (\function{time.sleep()},
152\method{\var{file}.read()}, \function{select.select()}) work as
153expected.)
154
155\item
156It is not possible to interrupt the \method{acquire()} method on a lock
157--- the \exception{KeyboardInterrupt} exception will happen after the
158lock has been acquired.
159
160\item
161When the main thread exits, it is system defined whether the other
162threads survive. On SGI IRIX using the native thread implementation,
163they survive. On most other systems, they are killed without
164executing \keyword{try} ... \keyword{finally} clauses or executing
165object destructors.
166\indexii{threads}{IRIX}
167
168\item
169When the main thread exits, it does not do any of its usual cleanup
170(except that \keyword{try} ... \keyword{finally} clauses are honored),
171and the standard I/O files are not flushed.
172
173\end{itemize}
Note: See TracBrowser for help on using the repository browser.