blob: d007eecde88a8fe7c080194dfeed3076922112ff [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{thread} ---
Fred Drake02e8c0f1999-04-21 18:01:14 +00002 Multiple threads of control}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake02e8c0f1999-04-21 18:01:14 +00004\declaremodule{builtin}{thread}
Fred Drake295da241998-08-10 19:42:37 +00005\modulesynopsis{Create multiple threads of control within one interpreter.}
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007
8This module provides low-level primitives for working with multiple
Guido van Rossum6bb1adc1995-03-13 10:03:32 +00009threads (a.k.a.\ \dfn{light-weight processes} or \dfn{tasks}) --- multiple
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000010threads of control sharing their global data space. For
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000011synchronization, simple locks (a.k.a.\ \dfn{mutexes} or \dfn{binary
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000012semaphores}) are provided.
Fred Drake61b04521998-01-20 05:52:23 +000013\index{light-weight processes}
14\index{processes, light-weight}
15\index{binary semaphores}
16\index{semaphores, binary}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000017
Fred Drake7d428ec2002-10-22 15:06:49 +000018The module is optional. It is supported on Windows, Linux, SGI
Fred Drake65b32f71998-02-09 20:27:12 +000019IRIX, Solaris 2.x, as well as on systems that have a \POSIX{} thread
Guido van Rossum29692332002-12-30 22:34:10 +000020(a.k.a. ``pthread'') implementation. For systems lacking the \module{thread}
Fred Drake740f8002002-12-30 23:00:36 +000021module, the \refmodule[dummythread]{dummy_thread} module is available.
22It duplicates this module's interface and can be
23used as a drop-in replacement.
Fred Drake61b04521998-01-20 05:52:23 +000024\index{pthreads}
Fred Drakec37b65e2001-11-28 07:26:15 +000025\indexii{threads}{\POSIX}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000026
27It defines the following constant and functions:
28
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000029\begin{excdesc}{error}
30Raised on thread-specific errors.
31\end{excdesc}
32
Guido van Rossum73d8bff1998-06-27 18:25:44 +000033\begin{datadesc}{LockType}
34This is the type of lock objects.
35\end{datadesc}
36
Fred Drake02e8c0f1999-04-21 18:01:14 +000037\begin{funcdesc}{start_new_thread}{function, args\optional{, kwargs}}
Guido van Rossum3c288632001-10-16 21:13:49 +000038Start 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
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000042terminates with an unhandled exception, a stack trace is printed and
43then the thread exits (but other threads continue to run).
44\end{funcdesc}
45
Kurt B. Kaiser0b1782f2003-07-08 17:07:20 +000046\begin{funcdesc}{interrupt_main}{}
George Yoshida90df06e2006-05-13 06:53:31 +000047Raise a \exception{KeyboardInterrupt} exception in the main thread. A subthread
48can use this function to interrupt the main thread.
Neal Norwitza4d2b862003-07-09 12:41:55 +000049\versionadded{2.3}
Kurt B. Kaiser0b1782f2003-07-08 17:07:20 +000050\end{funcdesc}
51
Guido van Rossum470be141995-03-17 16:07:09 +000052\begin{funcdesc}{exit}{}
Guido van Rossum73d8bff1998-06-27 18:25:44 +000053Raise the \exception{SystemExit} exception. When not caught, this
54will cause the thread to exit silently.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055\end{funcdesc}
56
Guido van Rossum470be141995-03-17 16:07:09 +000057%\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.
Fred Drake02e8c0f1999-04-21 18:01:14 +000060%\strong{Caveat:} code in pending \keyword{finally} clauses, in this thread
Guido van Rossum470be141995-03-17 16:07:09 +000061%or in other threads, is not executed.
62%\end{funcdesc}
63
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000064\begin{funcdesc}{allocate_lock}{}
65Return a new lock object. Methods of locks are described below. The
66lock is initially unlocked.
67\end{funcdesc}
68
Guido van Rossumb8b264b1994-08-12 13:13:50 +000069\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
Andrew MacIntyre92913322006-06-13 15:04:24 +000077\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
Fred Draked678cb71998-04-03 06:35:54 +000097
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000098Lock objects have the following methods:
99
Fred Draked678cb71998-04-03 06:35:54 +0000100\begin{methoddesc}[lock]{acquire}{\optional{waitflag}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000101Without 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
Andrew M. Kuchling921879a2005-06-02 16:59:18 +0000104reason for existence). If the integer
Fred Draked61975e1998-11-30 16:26:50 +0000105\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
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000107immediately without waiting, while if it is nonzero, the lock is
Andrew M. Kuchling921879a2005-06-02 16:59:18 +0000108acquired unconditionally as before. The
Neal Norwitz6b353702002-04-09 18:15:00 +0000109return value is \code{True} if the lock is acquired successfully,
110\code{False} if not.
Fred Draked678cb71998-04-03 06:35:54 +0000111\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000112
Fred Draked678cb71998-04-03 06:35:54 +0000113\begin{methoddesc}[lock]{release}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000114Releases the lock. The lock must have been acquired earlier, but not
115necessarily by the same thread.
Fred Draked678cb71998-04-03 06:35:54 +0000116\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000117
Fred Draked678cb71998-04-03 06:35:54 +0000118\begin{methoddesc}[lock]{locked}{}
Neal Norwitz6b353702002-04-09 18:15:00 +0000119Return the status of the lock:\ \code{True} if it has been acquired by
120some thread, \code{False} if not.
Fred Draked678cb71998-04-03 06:35:54 +0000121\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000122
Phillip J. Eby168e99f2006-03-28 00:13:10 +0000123In 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
Fred Drakeaf8a0151998-01-14 14:51:31 +0000136\strong{Caveats:}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000137
138\begin{itemize}
139\item
140Threads interact strangely with interrupts: the
Fred Draked678cb71998-04-03 06:35:54 +0000141\exception{KeyboardInterrupt} exception will be received by an
Fred Drake02e8c0f1999-04-21 18:01:14 +0000142arbitrary thread. (When the \refmodule{signal}\refbimodindex{signal}
Fred Draked678cb71998-04-03 06:35:54 +0000143module is available, interrupts always go to the main thread.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000144
145\item
Fred Draked678cb71998-04-03 06:35:54 +0000146Calling \function{sys.exit()} or raising the \exception{SystemExit}
Fred Drake43b89b62000-04-05 15:00:38 +0000147exception is equivalent to calling \function{exit()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000148
149\item
150Not all built-in functions that may block waiting for I/O allow other
Fred Draked678cb71998-04-03 06:35:54 +0000151threads to run. (The most popular ones (\function{time.sleep()},
152\method{\var{file}.read()}, \function{select.select()}) work as
153expected.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000154
Guido van Rossum3572d371997-08-14 19:51:26 +0000155\item
Fred Draked678cb71998-04-03 06:35:54 +0000156It 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.
Guido van Rossum3572d371997-08-14 19:51:26 +0000159
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
Fred Draked678cb71998-04-03 06:35:54 +0000164executing \keyword{try} ... \keyword{finally} clauses or executing
165object destructors.
Fred Drake61b04521998-01-20 05:52:23 +0000166\indexii{threads}{IRIX}
Guido van Rossum3572d371997-08-14 19:51:26 +0000167
168\item
Fred Draked678cb71998-04-03 06:35:54 +0000169When 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.
Guido van Rossum3572d371997-08-14 19:51:26 +0000172
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000173\end{itemize}