Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{thread}} |
| 2 | \bimodindex{thread} |
| 3 | |
| 4 | This module provides low-level primitives for working with multiple |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 5 | threads (a.k.a.\ \dfn{light-weight processes} or \dfn{tasks}) --- multiple |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 6 | threads of control sharing their global data space. For |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 7 | synchronization, simple locks (a.k.a.\ \dfn{mutexes} or \dfn{binary |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 8 | semaphores}) are provided. |
| 9 | |
Guido van Rossum | b8b264b | 1994-08-12 13:13:50 +0000 | [diff] [blame] | 10 | The module is optional and supported on SGI IRIX 4.x and 5.x and Sun |
| 11 | Solaris 2.x systems, as well as on systems that have a PTHREAD |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 12 | implementation (e.g.\ KSR). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 13 | |
| 14 | It defines the following constant and functions: |
| 15 | |
| 16 | \renewcommand{\indexsubitem}{(in module thread)} |
| 17 | \begin{excdesc}{error} |
| 18 | Raised on thread-specific errors. |
| 19 | \end{excdesc} |
| 20 | |
| 21 | \begin{funcdesc}{start_new_thread}{func\, arg} |
| 22 | Start a new thread. The thread executes the function \var{func} |
| 23 | with the argument list \var{arg} (which must be a tuple). When the |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 24 | function returns, the thread silently exits. When the function |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 25 | terminates with an unhandled exception, a stack trace is printed and |
| 26 | then the thread exits (but other threads continue to run). |
| 27 | \end{funcdesc} |
| 28 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 29 | \begin{funcdesc}{exit}{} |
| 30 | This is a shorthand for \code{thread.exit_thread()}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 31 | \end{funcdesc} |
| 32 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 33 | \begin{funcdesc}{exit_thread}{} |
| 34 | Raise the \code{SystemExit} exception. When not caught, this will |
| 35 | cause the thread to exit silently. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 36 | \end{funcdesc} |
| 37 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 38 | %\begin{funcdesc}{exit_prog}{status} |
| 39 | %Exit all threads and report the value of the integer argument |
| 40 | %\var{status} as the exit status of the entire program. |
| 41 | %\strong{Caveat:} code in pending \code{finally} clauses, in this thread |
| 42 | %or in other threads, is not executed. |
| 43 | %\end{funcdesc} |
| 44 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 45 | \begin{funcdesc}{allocate_lock}{} |
| 46 | Return a new lock object. Methods of locks are described below. The |
| 47 | lock is initially unlocked. |
| 48 | \end{funcdesc} |
| 49 | |
Guido van Rossum | b8b264b | 1994-08-12 13:13:50 +0000 | [diff] [blame] | 50 | \begin{funcdesc}{get_ident}{} |
| 51 | Return the `thread identifier' of the current thread. This is a |
| 52 | nonzero integer. Its value has no direct meaning; it is intended as a |
| 53 | magic cookie to be used e.g. to index a dictionary of thread-specific |
| 54 | data. Thread identifiers may be recycled when a thread exits and |
| 55 | another thread is created. |
| 56 | \end{funcdesc} |
| 57 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 58 | Lock objects have the following methods: |
| 59 | |
| 60 | \renewcommand{\indexsubitem}{(lock method)} |
Guido van Rossum | 96628a9 | 1995-04-10 11:34:00 +0000 | [diff] [blame] | 61 | \begin{funcdesc}{acquire}{\optional{waitflag}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 62 | Without the optional argument, this method acquires the lock |
| 63 | unconditionally, if necessary waiting until it is released by another |
| 64 | thread (only one thread at a time can acquire a lock --- that's their |
| 65 | reason for existence), and returns \code{None}. If the integer |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 66 | \var{waitflag} argument is present, the action depends on its value:\ |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 67 | if it is zero, the lock is only acquired if it can be acquired |
| 68 | immediately without waiting, while if it is nonzero, the lock is |
| 69 | acquired unconditionally as before. If an argument is present, the |
| 70 | return value is 1 if the lock is acquired successfully, 0 if not. |
| 71 | \end{funcdesc} |
| 72 | |
| 73 | \begin{funcdesc}{release}{} |
| 74 | Releases the lock. The lock must have been acquired earlier, but not |
| 75 | necessarily by the same thread. |
| 76 | \end{funcdesc} |
| 77 | |
| 78 | \begin{funcdesc}{locked}{} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 79 | Return the status of the lock:\ 1 if it has been acquired by some |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 80 | thread, 0 if not. |
| 81 | \end{funcdesc} |
| 82 | |
| 83 | {\bf Caveats:} |
| 84 | |
| 85 | \begin{itemize} |
| 86 | \item |
| 87 | Threads interact strangely with interrupts: the |
| 88 | \code{KeyboardInterrupt} exception will be received by an arbitrary |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 89 | thread. (When the \code{signal} module is available, interrupts |
| 90 | always go to the main thread.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 91 | |
| 92 | \item |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 93 | Calling \code{sys.exit()} or raising the \code{SystemExit} is |
| 94 | equivalent to calling \code{thread.exit_thread()}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 95 | |
| 96 | \item |
| 97 | Not all built-in functions that may block waiting for I/O allow other |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 98 | threads to run. (The most popular ones (\code{sleep}, \code{read}, |
| 99 | \code{select}) work as expected.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 100 | |
| 101 | \end{itemize} |