blob: 6dfd8758b7f84dd88882798f079fb8ad905db01a [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}
21module, the \refmodule{dummy_thread} module is available. It duplicates this
22module's interface and can be used as a drop-in replacement.
Fred Drake61b04521998-01-20 05:52:23 +000023\index{pthreads}
Fred Drakec37b65e2001-11-28 07:26:15 +000024\indexii{threads}{\POSIX}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000025
26It defines the following constant and functions:
27
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000028\begin{excdesc}{error}
29Raised on thread-specific errors.
30\end{excdesc}
31
Guido van Rossum73d8bff1998-06-27 18:25:44 +000032\begin{datadesc}{LockType}
33This is the type of lock objects.
34\end{datadesc}
35
Fred Drake02e8c0f1999-04-21 18:01:14 +000036\begin{funcdesc}{start_new_thread}{function, args\optional{, kwargs}}
Guido van Rossum3c288632001-10-16 21:13:49 +000037Start a new thread and return its identifier. The thread executes the function
38\var{function} with the argument list \var{args} (which must be a tuple). The
39optional \var{kwargs} argument specifies a dictionary of keyword arguments.
40When the function returns, the thread silently exits. When the function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000041terminates with an unhandled exception, a stack trace is printed and
42then the thread exits (but other threads continue to run).
43\end{funcdesc}
44
Guido van Rossum470be141995-03-17 16:07:09 +000045\begin{funcdesc}{exit}{}
Guido van Rossum73d8bff1998-06-27 18:25:44 +000046Raise the \exception{SystemExit} exception. When not caught, this
47will cause the thread to exit silently.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000048\end{funcdesc}
49
Guido van Rossum470be141995-03-17 16:07:09 +000050\begin{funcdesc}{exit_thread}{}
Fred Drake91e52111998-07-02 19:33:12 +000051\deprecated{1.5.2}{Use \function{exit()}.}
Guido van Rossum73d8bff1998-06-27 18:25:44 +000052This is an obsolete synonym for \function{exit()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000053\end{funcdesc}
54
Guido van Rossum470be141995-03-17 16:07:09 +000055%\begin{funcdesc}{exit_prog}{status}
56%Exit all threads and report the value of the integer argument
57%\var{status} as the exit status of the entire program.
Fred Drake02e8c0f1999-04-21 18:01:14 +000058%\strong{Caveat:} code in pending \keyword{finally} clauses, in this thread
Guido van Rossum470be141995-03-17 16:07:09 +000059%or in other threads, is not executed.
60%\end{funcdesc}
61
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000062\begin{funcdesc}{allocate_lock}{}
63Return a new lock object. Methods of locks are described below. The
64lock is initially unlocked.
65\end{funcdesc}
66
Guido van Rossumb8b264b1994-08-12 13:13:50 +000067\begin{funcdesc}{get_ident}{}
68Return the `thread identifier' of the current thread. This is a
69nonzero integer. Its value has no direct meaning; it is intended as a
70magic cookie to be used e.g. to index a dictionary of thread-specific
71data. Thread identifiers may be recycled when a thread exits and
72another thread is created.
73\end{funcdesc}
74
Fred Draked678cb71998-04-03 06:35:54 +000075
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000076Lock objects have the following methods:
77
Fred Draked678cb71998-04-03 06:35:54 +000078\begin{methoddesc}[lock]{acquire}{\optional{waitflag}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079Without the optional argument, this method acquires the lock
80unconditionally, if necessary waiting until it is released by another
81thread (only one thread at a time can acquire a lock --- that's their
82reason for existence), and returns \code{None}. If the integer
Fred Draked61975e1998-11-30 16:26:50 +000083\var{waitflag} argument is present, the action depends on its
84value: if it is zero, the lock is only acquired if it can be acquired
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000085immediately without waiting, while if it is nonzero, the lock is
86acquired unconditionally as before. If an argument is present, the
Neal Norwitz6b353702002-04-09 18:15:00 +000087return value is \code{True} if the lock is acquired successfully,
88\code{False} if not.
Fred Draked678cb71998-04-03 06:35:54 +000089\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000090
Fred Draked678cb71998-04-03 06:35:54 +000091\begin{methoddesc}[lock]{release}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000092Releases the lock. The lock must have been acquired earlier, but not
93necessarily by the same thread.
Fred Draked678cb71998-04-03 06:35:54 +000094\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000095
Fred Draked678cb71998-04-03 06:35:54 +000096\begin{methoddesc}[lock]{locked}{}
Neal Norwitz6b353702002-04-09 18:15:00 +000097Return the status of the lock:\ \code{True} if it has been acquired by
98some thread, \code{False} if not.
Fred Draked678cb71998-04-03 06:35:54 +000099\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000100
Fred Drakeaf8a0151998-01-14 14:51:31 +0000101\strong{Caveats:}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000102
103\begin{itemize}
104\item
105Threads interact strangely with interrupts: the
Fred Draked678cb71998-04-03 06:35:54 +0000106\exception{KeyboardInterrupt} exception will be received by an
Fred Drake02e8c0f1999-04-21 18:01:14 +0000107arbitrary thread. (When the \refmodule{signal}\refbimodindex{signal}
Fred Draked678cb71998-04-03 06:35:54 +0000108module is available, interrupts always go to the main thread.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000109
110\item
Fred Draked678cb71998-04-03 06:35:54 +0000111Calling \function{sys.exit()} or raising the \exception{SystemExit}
Fred Drake43b89b62000-04-05 15:00:38 +0000112exception is equivalent to calling \function{exit()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000113
114\item
115Not all built-in functions that may block waiting for I/O allow other
Fred Draked678cb71998-04-03 06:35:54 +0000116threads to run. (The most popular ones (\function{time.sleep()},
117\method{\var{file}.read()}, \function{select.select()}) work as
118expected.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000119
Guido van Rossum3572d371997-08-14 19:51:26 +0000120\item
Fred Draked678cb71998-04-03 06:35:54 +0000121It is not possible to interrupt the \method{acquire()} method on a lock
122--- the \exception{KeyboardInterrupt} exception will happen after the
123lock has been acquired.
Guido van Rossum3572d371997-08-14 19:51:26 +0000124
125\item
126When the main thread exits, it is system defined whether the other
127threads survive. On SGI IRIX using the native thread implementation,
128they survive. On most other systems, they are killed without
Fred Draked678cb71998-04-03 06:35:54 +0000129executing \keyword{try} ... \keyword{finally} clauses or executing
130object destructors.
Fred Drake61b04521998-01-20 05:52:23 +0000131\indexii{threads}{IRIX}
Guido van Rossum3572d371997-08-14 19:51:26 +0000132
133\item
Fred Draked678cb71998-04-03 06:35:54 +0000134When the main thread exits, it does not do any of its usual cleanup
135(except that \keyword{try} ... \keyword{finally} clauses are honored),
136and the standard I/O files are not flushed.
Guido van Rossum3572d371997-08-14 19:51:26 +0000137
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000138\end{itemize}