blob: c8d251e06e189433837d0395e79e5f7a6b16d819 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Built-in Module \module{thread}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-thread}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003\bimodindex{thread}
4
5This module provides low-level primitives for working with multiple
Guido van Rossum6bb1adc1995-03-13 10:03:32 +00006threads (a.k.a.\ \dfn{light-weight processes} or \dfn{tasks}) --- multiple
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007threads of control sharing their global data space. For
Guido van Rossum6bb1adc1995-03-13 10:03:32 +00008synchronization, simple locks (a.k.a.\ \dfn{mutexes} or \dfn{binary
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00009semaphores}) are provided.
Fred Drake61b04521998-01-20 05:52:23 +000010\index{light-weight processes}
11\index{processes, light-weight}
12\index{binary semaphores}
13\index{semaphores, binary}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000014
Guido van Rossum3572d371997-08-14 19:51:26 +000015The module is optional. It is supported on Windows NT and '95, SGI
Fred Drake65b32f71998-02-09 20:27:12 +000016IRIX, Solaris 2.x, as well as on systems that have a \POSIX{} thread
Guido van Rossum3572d371997-08-14 19:51:26 +000017(a.k.a. ``pthread'') implementation.
Fred Drake61b04521998-01-20 05:52:23 +000018\index{pthreads}
Fred Drake1624a501998-02-09 22:12:28 +000019\indexii{threads}{\POSIX{}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020
21It defines the following constant and functions:
22
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000023\begin{excdesc}{error}
24Raised on thread-specific errors.
25\end{excdesc}
26
Guido van Rossum73d8bff1998-06-27 18:25:44 +000027\begin{datadesc}{LockType}
28This is the type of lock objects.
29\end{datadesc}
30
31\begin{funcdesc}{start_new_thread}{function, args\optional{kwargs}}
32Start a new thread. The thread executes the function \var{function}
33with the argument list \var{args} (which must be a tuple). The
34optional \var{kwargs} argument specifies a dictionary of keyword
35arguments. When the
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000036function returns, the thread silently exits. When the function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000037terminates with an unhandled exception, a stack trace is printed and
38then the thread exits (but other threads continue to run).
39\end{funcdesc}
40
Guido van Rossum470be141995-03-17 16:07:09 +000041\begin{funcdesc}{exit}{}
Guido van Rossum73d8bff1998-06-27 18:25:44 +000042Raise the \exception{SystemExit} exception. When not caught, this
43will cause the thread to exit silently.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000044\end{funcdesc}
45
Guido van Rossum470be141995-03-17 16:07:09 +000046\begin{funcdesc}{exit_thread}{}
Fred Drake91e52111998-07-02 19:33:12 +000047\deprecated{1.5.2}{Use \function{exit()}.}
Guido van Rossum73d8bff1998-06-27 18:25:44 +000048This is an obsolete synonym for \function{exit()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000049\end{funcdesc}
50
Guido van Rossum470be141995-03-17 16:07:09 +000051%\begin{funcdesc}{exit_prog}{status}
52%Exit all threads and report the value of the integer argument
53%\var{status} as the exit status of the entire program.
54%\strong{Caveat:} code in pending \code{finally} clauses, in this thread
55%or in other threads, is not executed.
56%\end{funcdesc}
57
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000058\begin{funcdesc}{allocate_lock}{}
59Return a new lock object. Methods of locks are described below. The
60lock is initially unlocked.
61\end{funcdesc}
62
Guido van Rossumb8b264b1994-08-12 13:13:50 +000063\begin{funcdesc}{get_ident}{}
64Return the `thread identifier' of the current thread. This is a
65nonzero integer. Its value has no direct meaning; it is intended as a
66magic cookie to be used e.g. to index a dictionary of thread-specific
67data. Thread identifiers may be recycled when a thread exits and
68another thread is created.
69\end{funcdesc}
70
Fred Draked678cb71998-04-03 06:35:54 +000071
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000072Lock objects have the following methods:
73
Fred Draked678cb71998-04-03 06:35:54 +000074\begin{methoddesc}[lock]{acquire}{\optional{waitflag}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000075Without the optional argument, this method acquires the lock
76unconditionally, if necessary waiting until it is released by another
77thread (only one thread at a time can acquire a lock --- that's their
78reason for existence), and returns \code{None}. If the integer
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000079\var{waitflag} argument is present, the action depends on its value:\
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000080if it is zero, the lock is only acquired if it can be acquired
81immediately without waiting, while if it is nonzero, the lock is
82acquired unconditionally as before. If an argument is present, the
Fred Draked678cb71998-04-03 06:35:54 +000083return value is \code{1} if the lock is acquired successfully,
84\code{0} if not.
85\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000086
Fred Draked678cb71998-04-03 06:35:54 +000087\begin{methoddesc}[lock]{release}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000088Releases the lock. The lock must have been acquired earlier, but not
89necessarily by the same thread.
Fred Draked678cb71998-04-03 06:35:54 +000090\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000091
Fred Draked678cb71998-04-03 06:35:54 +000092\begin{methoddesc}[lock]{locked}{}
93Return the status of the lock:\ \code{1} if it has been acquired by
94some thread, \code{0} if not.
95\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000096
Fred Drakeaf8a0151998-01-14 14:51:31 +000097\strong{Caveats:}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000098
99\begin{itemize}
100\item
101Threads interact strangely with interrupts: the
Fred Draked678cb71998-04-03 06:35:54 +0000102\exception{KeyboardInterrupt} exception will be received by an
103arbitrary thread. (When the \module{signal}\refbimodindex{signal}
104module is available, interrupts always go to the main thread.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000105
106\item
Fred Draked678cb71998-04-03 06:35:54 +0000107Calling \function{sys.exit()} or raising the \exception{SystemExit}
108exception is equivalent to calling \function{exit_thread()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000109
110\item
111Not all built-in functions that may block waiting for I/O allow other
Fred Draked678cb71998-04-03 06:35:54 +0000112threads to run. (The most popular ones (\function{time.sleep()},
113\method{\var{file}.read()}, \function{select.select()}) work as
114expected.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000115
Guido van Rossum3572d371997-08-14 19:51:26 +0000116\item
Fred Draked678cb71998-04-03 06:35:54 +0000117It is not possible to interrupt the \method{acquire()} method on a lock
118--- the \exception{KeyboardInterrupt} exception will happen after the
119lock has been acquired.
Guido van Rossum3572d371997-08-14 19:51:26 +0000120
121\item
122When the main thread exits, it is system defined whether the other
123threads survive. On SGI IRIX using the native thread implementation,
124they survive. On most other systems, they are killed without
Fred Draked678cb71998-04-03 06:35:54 +0000125executing \keyword{try} ... \keyword{finally} clauses or executing
126object destructors.
Fred Drake61b04521998-01-20 05:52:23 +0000127\indexii{threads}{IRIX}
Guido van Rossum3572d371997-08-14 19:51:26 +0000128
129\item
Fred Draked678cb71998-04-03 06:35:54 +0000130When the main thread exits, it does not do any of its usual cleanup
131(except that \keyword{try} ... \keyword{finally} clauses are honored),
132and the standard I/O files are not flushed.
Guido van Rossum3572d371997-08-14 19:51:26 +0000133
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000134\end{itemize}