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 |
| 5 | threads (a.k.a. \dfn{light-weight processes} or \dfn{tasks}) --- multiple |
| 6 | threads of control sharing their global data space. For |
| 7 | synchronization, simple locks (a.k.a. \dfn{mutexes} or \dfn{binary |
| 8 | semaphores}) are provided. |
| 9 | |
| 10 | The module is optional and supported on SGI and Sun Sparc systems only. |
| 11 | |
| 12 | It defines the following constant and functions: |
| 13 | |
| 14 | \renewcommand{\indexsubitem}{(in module thread)} |
| 15 | \begin{excdesc}{error} |
| 16 | Raised on thread-specific errors. |
| 17 | \end{excdesc} |
| 18 | |
| 19 | \begin{funcdesc}{start_new_thread}{func\, arg} |
| 20 | Start a new thread. The thread executes the function \var{func} |
| 21 | with the argument list \var{arg} (which must be a tuple). When the |
| 22 | function returns, the thread silently exits. When the function raises |
| 23 | terminates with an unhandled exception, a stack trace is printed and |
| 24 | then the thread exits (but other threads continue to run). |
| 25 | \end{funcdesc} |
| 26 | |
| 27 | \begin{funcdesc}{exit_thread}{} |
| 28 | Exit the current thread silently. Other threads continue to run. |
| 29 | \strong{Caveat:} code in pending \code{finally} clauses is not executed. |
| 30 | \end{funcdesc} |
| 31 | |
| 32 | \begin{funcdesc}{exit_prog}{status} |
| 33 | Exit all threads and report the value of the integer argument |
| 34 | \var{status} as the exit status of the entire program. |
| 35 | \strong{Caveat:} code in pending \code{finally} clauses, in this thread |
| 36 | or in other threads, is not executed. |
| 37 | \end{funcdesc} |
| 38 | |
| 39 | \begin{funcdesc}{allocate_lock}{} |
| 40 | Return a new lock object. Methods of locks are described below. The |
| 41 | lock is initially unlocked. |
| 42 | \end{funcdesc} |
| 43 | |
| 44 | Lock objects have the following methods: |
| 45 | |
| 46 | \renewcommand{\indexsubitem}{(lock method)} |
| 47 | \begin{funcdesc}{acquire}{waitflag} |
| 48 | Without the optional argument, this method acquires the lock |
| 49 | unconditionally, if necessary waiting until it is released by another |
| 50 | thread (only one thread at a time can acquire a lock --- that's their |
| 51 | reason for existence), and returns \code{None}. If the integer |
| 52 | \var{waitflag} argument is present, the action depends on its value: |
| 53 | if it is zero, the lock is only acquired if it can be acquired |
| 54 | immediately without waiting, while if it is nonzero, the lock is |
| 55 | acquired unconditionally as before. If an argument is present, the |
| 56 | return value is 1 if the lock is acquired successfully, 0 if not. |
| 57 | \end{funcdesc} |
| 58 | |
| 59 | \begin{funcdesc}{release}{} |
| 60 | Releases the lock. The lock must have been acquired earlier, but not |
| 61 | necessarily by the same thread. |
| 62 | \end{funcdesc} |
| 63 | |
| 64 | \begin{funcdesc}{locked}{} |
| 65 | Return the status of the lock: 1 if it has been acquired by some |
| 66 | thread, 0 if not. |
| 67 | \end{funcdesc} |
| 68 | |
| 69 | {\bf Caveats:} |
| 70 | |
| 71 | \begin{itemize} |
| 72 | \item |
| 73 | Threads interact strangely with interrupts: the |
| 74 | \code{KeyboardInterrupt} exception will be received by an arbitrary |
| 75 | thread. |
| 76 | |
| 77 | \item |
| 78 | Calling \code{sys.exit(\var{status})} or executing |
| 79 | \code{raise SystemExit, \var{status}} is almost equivalent to calling |
| 80 | \code{thread.exit_prog(\var{status})}, except that the former ways of |
| 81 | exiting the entire program do honor \code{finally} clauses in the |
| 82 | current thread (but not in other threads). |
| 83 | |
| 84 | \item |
| 85 | Not all built-in functions that may block waiting for I/O allow other |
| 86 | threads to run, although the most popular ones (\code{sleep}, |
| 87 | \code{read}, \code{select}) work as expected. |
| 88 | |
| 89 | \end{itemize} |