blob: 9bc1849b521f13211f3c19d00f2d95969f684a4a [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}{}
Guido van Rossum73d8bff1998-06-27 18:25:44 +000047This is an obsolete synonym for \function{exit()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000048\end{funcdesc}
49
Guido van Rossum470be141995-03-17 16:07:09 +000050%\begin{funcdesc}{exit_prog}{status}
51%Exit all threads and report the value of the integer argument
52%\var{status} as the exit status of the entire program.
53%\strong{Caveat:} code in pending \code{finally} clauses, in this thread
54%or in other threads, is not executed.
55%\end{funcdesc}
56
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000057\begin{funcdesc}{allocate_lock}{}
58Return a new lock object. Methods of locks are described below. The
59lock is initially unlocked.
60\end{funcdesc}
61
Guido van Rossumb8b264b1994-08-12 13:13:50 +000062\begin{funcdesc}{get_ident}{}
63Return the `thread identifier' of the current thread. This is a
64nonzero integer. Its value has no direct meaning; it is intended as a
65magic cookie to be used e.g. to index a dictionary of thread-specific
66data. Thread identifiers may be recycled when a thread exits and
67another thread is created.
68\end{funcdesc}
69
Fred Draked678cb71998-04-03 06:35:54 +000070
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000071Lock objects have the following methods:
72
Fred Draked678cb71998-04-03 06:35:54 +000073\begin{methoddesc}[lock]{acquire}{\optional{waitflag}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000074Without the optional argument, this method acquires the lock
75unconditionally, if necessary waiting until it is released by another
76thread (only one thread at a time can acquire a lock --- that's their
77reason for existence), and returns \code{None}. If the integer
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000078\var{waitflag} argument is present, the action depends on its value:\
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079if it is zero, the lock is only acquired if it can be acquired
80immediately without waiting, while if it is nonzero, the lock is
81acquired unconditionally as before. If an argument is present, the
Fred Draked678cb71998-04-03 06:35:54 +000082return value is \code{1} if the lock is acquired successfully,
83\code{0} if not.
84\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000085
Fred Draked678cb71998-04-03 06:35:54 +000086\begin{methoddesc}[lock]{release}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000087Releases the lock. The lock must have been acquired earlier, but not
88necessarily by the same thread.
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]{locked}{}
92Return the status of the lock:\ \code{1} if it has been acquired by
93some thread, \code{0} if not.
94\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000095
Fred Drakeaf8a0151998-01-14 14:51:31 +000096\strong{Caveats:}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000097
98\begin{itemize}
99\item
100Threads interact strangely with interrupts: the
Fred Draked678cb71998-04-03 06:35:54 +0000101\exception{KeyboardInterrupt} exception will be received by an
102arbitrary thread. (When the \module{signal}\refbimodindex{signal}
103module is available, interrupts always go to the main thread.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000104
105\item
Fred Draked678cb71998-04-03 06:35:54 +0000106Calling \function{sys.exit()} or raising the \exception{SystemExit}
107exception is equivalent to calling \function{exit_thread()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000108
109\item
110Not all built-in functions that may block waiting for I/O allow other
Fred Draked678cb71998-04-03 06:35:54 +0000111threads to run. (The most popular ones (\function{time.sleep()},
112\method{\var{file}.read()}, \function{select.select()}) work as
113expected.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000114
Guido van Rossum3572d371997-08-14 19:51:26 +0000115\item
Fred Draked678cb71998-04-03 06:35:54 +0000116It is not possible to interrupt the \method{acquire()} method on a lock
117--- the \exception{KeyboardInterrupt} exception will happen after the
118lock has been acquired.
Guido van Rossum3572d371997-08-14 19:51:26 +0000119
120\item
121When the main thread exits, it is system defined whether the other
122threads survive. On SGI IRIX using the native thread implementation,
123they survive. On most other systems, they are killed without
Fred Draked678cb71998-04-03 06:35:54 +0000124executing \keyword{try} ... \keyword{finally} clauses or executing
125object destructors.
Fred Drake61b04521998-01-20 05:52:23 +0000126\indexii{threads}{IRIX}
Guido van Rossum3572d371997-08-14 19:51:26 +0000127
128\item
Fred Draked678cb71998-04-03 06:35:54 +0000129When the main thread exits, it does not do any of its usual cleanup
130(except that \keyword{try} ... \keyword{finally} clauses are honored),
131and the standard I/O files are not flushed.
Guido van Rossum3572d371997-08-14 19:51:26 +0000132
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000133\end{itemize}