blob: 9e9d8ba0ab8a164ba9d33f9d5ae0c966853a2790 [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
Fred Drakecce10901998-03-17 06:33:25 +000027\begin{funcdesc}{start_new_thread}{func, arg}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000028Start a new thread. The thread executes the function \var{func}
29with the argument list \var{arg} (which must be a tuple). When the
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000030function returns, the thread silently exits. When the function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000031terminates with an unhandled exception, a stack trace is printed and
32then the thread exits (but other threads continue to run).
33\end{funcdesc}
34
Guido van Rossum470be141995-03-17 16:07:09 +000035\begin{funcdesc}{exit}{}
Fred Draked678cb71998-04-03 06:35:54 +000036This is a shorthand for \function{exit_thread()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000037\end{funcdesc}
38
Guido van Rossum470be141995-03-17 16:07:09 +000039\begin{funcdesc}{exit_thread}{}
Fred Draked678cb71998-04-03 06:35:54 +000040Raise the \exception{SystemExit} exception. When not caught, this
41will cause the thread to exit silently.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000042\end{funcdesc}
43
Guido van Rossum470be141995-03-17 16:07:09 +000044%\begin{funcdesc}{exit_prog}{status}
45%Exit all threads and report the value of the integer argument
46%\var{status} as the exit status of the entire program.
47%\strong{Caveat:} code in pending \code{finally} clauses, in this thread
48%or in other threads, is not executed.
49%\end{funcdesc}
50
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000051\begin{funcdesc}{allocate_lock}{}
52Return a new lock object. Methods of locks are described below. The
53lock is initially unlocked.
54\end{funcdesc}
55
Guido van Rossumb8b264b1994-08-12 13:13:50 +000056\begin{funcdesc}{get_ident}{}
57Return the `thread identifier' of the current thread. This is a
58nonzero integer. Its value has no direct meaning; it is intended as a
59magic cookie to be used e.g. to index a dictionary of thread-specific
60data. Thread identifiers may be recycled when a thread exits and
61another thread is created.
62\end{funcdesc}
63
Fred Draked678cb71998-04-03 06:35:54 +000064
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000065Lock objects have the following methods:
66
Fred Draked678cb71998-04-03 06:35:54 +000067\begin{methoddesc}[lock]{acquire}{\optional{waitflag}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000068Without the optional argument, this method acquires the lock
69unconditionally, if necessary waiting until it is released by another
70thread (only one thread at a time can acquire a lock --- that's their
71reason for existence), and returns \code{None}. If the integer
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000072\var{waitflag} argument is present, the action depends on its value:\
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000073if it is zero, the lock is only acquired if it can be acquired
74immediately without waiting, while if it is nonzero, the lock is
75acquired unconditionally as before. If an argument is present, the
Fred Draked678cb71998-04-03 06:35:54 +000076return value is \code{1} if the lock is acquired successfully,
77\code{0} if not.
78\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079
Fred Draked678cb71998-04-03 06:35:54 +000080\begin{methoddesc}[lock]{release}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000081Releases the lock. The lock must have been acquired earlier, but not
82necessarily by the same thread.
Fred Draked678cb71998-04-03 06:35:54 +000083\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000084
Fred Draked678cb71998-04-03 06:35:54 +000085\begin{methoddesc}[lock]{locked}{}
86Return the status of the lock:\ \code{1} if it has been acquired by
87some thread, \code{0} if not.
88\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000089
Fred Drakeaf8a0151998-01-14 14:51:31 +000090\strong{Caveats:}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000091
92\begin{itemize}
93\item
94Threads interact strangely with interrupts: the
Fred Draked678cb71998-04-03 06:35:54 +000095\exception{KeyboardInterrupt} exception will be received by an
96arbitrary thread. (When the \module{signal}\refbimodindex{signal}
97module is available, interrupts always go to the main thread.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000098
99\item
Fred Draked678cb71998-04-03 06:35:54 +0000100Calling \function{sys.exit()} or raising the \exception{SystemExit}
101exception is equivalent to calling \function{exit_thread()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000102
103\item
104Not all built-in functions that may block waiting for I/O allow other
Fred Draked678cb71998-04-03 06:35:54 +0000105threads to run. (The most popular ones (\function{time.sleep()},
106\method{\var{file}.read()}, \function{select.select()}) work as
107expected.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000108
Guido van Rossum3572d371997-08-14 19:51:26 +0000109\item
Fred Draked678cb71998-04-03 06:35:54 +0000110It is not possible to interrupt the \method{acquire()} method on a lock
111--- the \exception{KeyboardInterrupt} exception will happen after the
112lock has been acquired.
Guido van Rossum3572d371997-08-14 19:51:26 +0000113
114\item
115When the main thread exits, it is system defined whether the other
116threads survive. On SGI IRIX using the native thread implementation,
117they survive. On most other systems, they are killed without
Fred Draked678cb71998-04-03 06:35:54 +0000118executing \keyword{try} ... \keyword{finally} clauses or executing
119object destructors.
Fred Drake61b04521998-01-20 05:52:23 +0000120\indexii{threads}{IRIX}
Guido van Rossum3572d371997-08-14 19:51:26 +0000121
122\item
Fred Draked678cb71998-04-03 06:35:54 +0000123When the main thread exits, it does not do any of its usual cleanup
124(except that \keyword{try} ... \keyword{finally} clauses are honored),
125and the standard I/O files are not flushed.
Guido van Rossum3572d371997-08-14 19:51:26 +0000126
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000127\end{itemize}