Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Built-in Module \module{thread}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-thread} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 3 | \bimodindex{thread} |
| 4 | |
| 5 | This module provides low-level primitives for working with multiple |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 6 | 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] | 7 | threads of control sharing their global data space. For |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 8 | synchronization, simple locks (a.k.a.\ \dfn{mutexes} or \dfn{binary |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 9 | semaphores}) are provided. |
Fred Drake | 61b0452 | 1998-01-20 05:52:23 +0000 | [diff] [blame] | 10 | \index{light-weight processes} |
| 11 | \index{processes, light-weight} |
| 12 | \index{binary semaphores} |
| 13 | \index{semaphores, binary} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 3572d37 | 1997-08-14 19:51:26 +0000 | [diff] [blame] | 15 | The module is optional. It is supported on Windows NT and '95, SGI |
Fred Drake | 65b32f7 | 1998-02-09 20:27:12 +0000 | [diff] [blame] | 16 | IRIX, Solaris 2.x, as well as on systems that have a \POSIX{} thread |
Guido van Rossum | 3572d37 | 1997-08-14 19:51:26 +0000 | [diff] [blame] | 17 | (a.k.a. ``pthread'') implementation. |
Fred Drake | 61b0452 | 1998-01-20 05:52:23 +0000 | [diff] [blame] | 18 | \index{pthreads} |
Fred Drake | 1624a50 | 1998-02-09 22:12:28 +0000 | [diff] [blame] | 19 | \indexii{threads}{\POSIX{}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 20 | |
| 21 | It defines the following constant and functions: |
| 22 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 23 | \begin{excdesc}{error} |
| 24 | Raised on thread-specific errors. |
| 25 | \end{excdesc} |
| 26 | |
Guido van Rossum | 73d8bff | 1998-06-27 18:25:44 +0000 | [diff] [blame] | 27 | \begin{datadesc}{LockType} |
| 28 | This is the type of lock objects. |
| 29 | \end{datadesc} |
| 30 | |
| 31 | \begin{funcdesc}{start_new_thread}{function, args\optional{kwargs}} |
| 32 | Start a new thread. The thread executes the function \var{function} |
| 33 | with the argument list \var{args} (which must be a tuple). The |
| 34 | optional \var{kwargs} argument specifies a dictionary of keyword |
| 35 | arguments. When the |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 36 | function returns, the thread silently exits. When the function |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 37 | terminates with an unhandled exception, a stack trace is printed and |
| 38 | then the thread exits (but other threads continue to run). |
| 39 | \end{funcdesc} |
| 40 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 41 | \begin{funcdesc}{exit}{} |
Guido van Rossum | 73d8bff | 1998-06-27 18:25:44 +0000 | [diff] [blame] | 42 | Raise the \exception{SystemExit} exception. When not caught, this |
| 43 | will cause the thread to exit silently. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 44 | \end{funcdesc} |
| 45 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 46 | \begin{funcdesc}{exit_thread}{} |
Fred Drake | 91e5211 | 1998-07-02 19:33:12 +0000 | [diff] [blame] | 47 | \deprecated{1.5.2}{Use \function{exit()}.} |
Guido van Rossum | 73d8bff | 1998-06-27 18:25:44 +0000 | [diff] [blame] | 48 | This is an obsolete synonym for \function{exit()}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 49 | \end{funcdesc} |
| 50 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 51 | %\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 Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 58 | \begin{funcdesc}{allocate_lock}{} |
| 59 | Return a new lock object. Methods of locks are described below. The |
| 60 | lock is initially unlocked. |
| 61 | \end{funcdesc} |
| 62 | |
Guido van Rossum | b8b264b | 1994-08-12 13:13:50 +0000 | [diff] [blame] | 63 | \begin{funcdesc}{get_ident}{} |
| 64 | Return the `thread identifier' of the current thread. This is a |
| 65 | nonzero integer. Its value has no direct meaning; it is intended as a |
| 66 | magic cookie to be used e.g. to index a dictionary of thread-specific |
| 67 | data. Thread identifiers may be recycled when a thread exits and |
| 68 | another thread is created. |
| 69 | \end{funcdesc} |
| 70 | |
Fred Drake | d678cb7 | 1998-04-03 06:35:54 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 72 | Lock objects have the following methods: |
| 73 | |
Fred Drake | d678cb7 | 1998-04-03 06:35:54 +0000 | [diff] [blame] | 74 | \begin{methoddesc}[lock]{acquire}{\optional{waitflag}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 75 | Without the optional argument, this method acquires the lock |
| 76 | unconditionally, if necessary waiting until it is released by another |
| 77 | thread (only one thread at a time can acquire a lock --- that's their |
| 78 | reason for existence), and returns \code{None}. If the integer |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 79 | \var{waitflag} argument is present, the action depends on its value:\ |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 80 | if it is zero, the lock is only acquired if it can be acquired |
| 81 | immediately without waiting, while if it is nonzero, the lock is |
| 82 | acquired unconditionally as before. If an argument is present, the |
Fred Drake | d678cb7 | 1998-04-03 06:35:54 +0000 | [diff] [blame] | 83 | return value is \code{1} if the lock is acquired successfully, |
| 84 | \code{0} if not. |
| 85 | \end{methoddesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 86 | |
Fred Drake | d678cb7 | 1998-04-03 06:35:54 +0000 | [diff] [blame] | 87 | \begin{methoddesc}[lock]{release}{} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 88 | Releases the lock. The lock must have been acquired earlier, but not |
| 89 | necessarily by the same thread. |
Fred Drake | d678cb7 | 1998-04-03 06:35:54 +0000 | [diff] [blame] | 90 | \end{methoddesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 91 | |
Fred Drake | d678cb7 | 1998-04-03 06:35:54 +0000 | [diff] [blame] | 92 | \begin{methoddesc}[lock]{locked}{} |
| 93 | Return the status of the lock:\ \code{1} if it has been acquired by |
| 94 | some thread, \code{0} if not. |
| 95 | \end{methoddesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 96 | |
Fred Drake | af8a015 | 1998-01-14 14:51:31 +0000 | [diff] [blame] | 97 | \strong{Caveats:} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 98 | |
| 99 | \begin{itemize} |
| 100 | \item |
| 101 | Threads interact strangely with interrupts: the |
Fred Drake | d678cb7 | 1998-04-03 06:35:54 +0000 | [diff] [blame] | 102 | \exception{KeyboardInterrupt} exception will be received by an |
| 103 | arbitrary thread. (When the \module{signal}\refbimodindex{signal} |
| 104 | module is available, interrupts always go to the main thread.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 105 | |
| 106 | \item |
Fred Drake | d678cb7 | 1998-04-03 06:35:54 +0000 | [diff] [blame] | 107 | Calling \function{sys.exit()} or raising the \exception{SystemExit} |
| 108 | exception is equivalent to calling \function{exit_thread()}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 109 | |
| 110 | \item |
| 111 | Not all built-in functions that may block waiting for I/O allow other |
Fred Drake | d678cb7 | 1998-04-03 06:35:54 +0000 | [diff] [blame] | 112 | threads to run. (The most popular ones (\function{time.sleep()}, |
| 113 | \method{\var{file}.read()}, \function{select.select()}) work as |
| 114 | expected.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | 3572d37 | 1997-08-14 19:51:26 +0000 | [diff] [blame] | 116 | \item |
Fred Drake | d678cb7 | 1998-04-03 06:35:54 +0000 | [diff] [blame] | 117 | It is not possible to interrupt the \method{acquire()} method on a lock |
| 118 | --- the \exception{KeyboardInterrupt} exception will happen after the |
| 119 | lock has been acquired. |
Guido van Rossum | 3572d37 | 1997-08-14 19:51:26 +0000 | [diff] [blame] | 120 | |
| 121 | \item |
| 122 | When the main thread exits, it is system defined whether the other |
| 123 | threads survive. On SGI IRIX using the native thread implementation, |
| 124 | they survive. On most other systems, they are killed without |
Fred Drake | d678cb7 | 1998-04-03 06:35:54 +0000 | [diff] [blame] | 125 | executing \keyword{try} ... \keyword{finally} clauses or executing |
| 126 | object destructors. |
Fred Drake | 61b0452 | 1998-01-20 05:52:23 +0000 | [diff] [blame] | 127 | \indexii{threads}{IRIX} |
Guido van Rossum | 3572d37 | 1997-08-14 19:51:26 +0000 | [diff] [blame] | 128 | |
| 129 | \item |
Fred Drake | d678cb7 | 1998-04-03 06:35:54 +0000 | [diff] [blame] | 130 | When the main thread exits, it does not do any of its usual cleanup |
| 131 | (except that \keyword{try} ... \keyword{finally} clauses are honored), |
| 132 | and the standard I/O files are not flushed. |
Guido van Rossum | 3572d37 | 1997-08-14 19:51:26 +0000 | [diff] [blame] | 133 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 134 | \end{itemize} |