Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 1 | :mod:`_thread` --- Low-level threading API |
| 2 | ========================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 4 | .. module:: _thread |
| 5 | :synopsis: Low-level threading API. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 6 | |
| 7 | |
| 8 | .. index:: |
| 9 | single: light-weight processes |
| 10 | single: processes, light-weight |
| 11 | single: binary semaphores |
| 12 | single: semaphores, binary |
| 13 | |
| 14 | This module provides low-level primitives for working with multiple threads |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 15 | (also called :dfn:`light-weight processes` or :dfn:`tasks`) --- multiple threads of |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | control sharing their global data space. For synchronization, simple locks |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 17 | (also called :dfn:`mutexes` or :dfn:`binary semaphores`) are provided. |
| 18 | The :mod:`threading` module provides an easier to use and higher-level |
| 19 | threading API built on top of this module. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 20 | |
| 21 | .. index:: |
| 22 | single: pthreads |
| 23 | pair: threads; POSIX |
| 24 | |
| 25 | The module is optional. It is supported on Windows, Linux, SGI IRIX, Solaris |
| 26 | 2.x, as well as on systems that have a POSIX thread (a.k.a. "pthread") |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 27 | implementation. For systems lacking the :mod:`_thread` module, the |
| 28 | :mod:`_dummy_thread` module is available. It duplicates this module's interface |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 | and can be used as a drop-in replacement. |
| 30 | |
| 31 | It defines the following constant and functions: |
| 32 | |
| 33 | |
| 34 | .. exception:: error |
| 35 | |
| 36 | Raised on thread-specific errors. |
| 37 | |
| 38 | |
| 39 | .. data:: LockType |
| 40 | |
| 41 | This is the type of lock objects. |
| 42 | |
| 43 | |
| 44 | .. function:: start_new_thread(function, args[, kwargs]) |
| 45 | |
| 46 | Start a new thread and return its identifier. The thread executes the function |
| 47 | *function* with the argument list *args* (which must be a tuple). The optional |
| 48 | *kwargs* argument specifies a dictionary of keyword arguments. When the function |
| 49 | returns, the thread silently exits. When the function terminates with an |
| 50 | unhandled exception, a stack trace is printed and then the thread exits (but |
| 51 | other threads continue to run). |
| 52 | |
| 53 | |
| 54 | .. function:: interrupt_main() |
| 55 | |
| 56 | Raise a :exc:`KeyboardInterrupt` exception in the main thread. A subthread can |
| 57 | use this function to interrupt the main thread. |
| 58 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
| 60 | .. function:: exit() |
| 61 | |
| 62 | Raise the :exc:`SystemExit` exception. When not caught, this will cause the |
| 63 | thread to exit silently. |
| 64 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 65 | .. |
| 66 | function:: exit_prog(status) |
| 67 | |
| 68 | Exit all threads and report the value of the integer argument |
| 69 | *status* as the exit status of the entire program. |
| 70 | **Caveat:** code in pending :keyword:`finally` clauses, in this thread |
| 71 | or in other threads, is not executed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 72 | |
| 73 | |
| 74 | .. function:: allocate_lock() |
| 75 | |
| 76 | Return a new lock object. Methods of locks are described below. The lock is |
| 77 | initially unlocked. |
| 78 | |
| 79 | |
| 80 | .. function:: get_ident() |
| 81 | |
| 82 | Return the 'thread identifier' of the current thread. This is a nonzero |
| 83 | integer. Its value has no direct meaning; it is intended as a magic cookie to |
| 84 | be used e.g. to index a dictionary of thread-specific data. Thread identifiers |
| 85 | may be recycled when a thread exits and another thread is created. |
| 86 | |
| 87 | |
| 88 | .. function:: stack_size([size]) |
| 89 | |
| 90 | Return the thread stack size used when creating new threads. The optional |
| 91 | *size* argument specifies the stack size to be used for subsequently created |
| 92 | threads, and must be 0 (use platform or configured default) or a positive |
| 93 | integer value of at least 32,768 (32kB). If changing the thread stack size is |
| 94 | unsupported, a :exc:`ThreadError` is raised. If the specified stack size is |
| 95 | invalid, a :exc:`ValueError` is raised and the stack size is unmodified. 32kB |
| 96 | is currently the minimum supported stack size value to guarantee sufficient |
| 97 | stack space for the interpreter itself. Note that some platforms may have |
| 98 | particular restrictions on values for the stack size, such as requiring a |
| 99 | minimum stack size > 32kB or requiring allocation in multiples of the system |
| 100 | memory page size - platform documentation should be referred to for more |
| 101 | information (4kB pages are common; using multiples of 4096 for the stack size is |
| 102 | the suggested approach in the absence of more specific information). |
| 103 | Availability: Windows, systems with POSIX threads. |
| 104 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 105 | |
| 106 | Lock objects have the following methods: |
| 107 | |
| 108 | |
| 109 | .. method:: lock.acquire([waitflag]) |
| 110 | |
| 111 | Without the optional argument, this method acquires the lock unconditionally, if |
| 112 | necessary waiting until it is released by another thread (only one thread at a |
| 113 | time can acquire a lock --- that's their reason for existence). If the integer |
| 114 | *waitflag* argument is present, the action depends on its value: if it is zero, |
| 115 | the lock is only acquired if it can be acquired immediately without waiting, |
| 116 | while if it is nonzero, the lock is acquired unconditionally as before. The |
| 117 | return value is ``True`` if the lock is acquired successfully, ``False`` if not. |
| 118 | |
| 119 | |
| 120 | .. method:: lock.release() |
| 121 | |
| 122 | Releases the lock. The lock must have been acquired earlier, but not |
| 123 | necessarily by the same thread. |
| 124 | |
| 125 | |
| 126 | .. method:: lock.locked() |
| 127 | |
| 128 | Return the status of the lock: ``True`` if it has been acquired by some thread, |
| 129 | ``False`` if not. |
| 130 | |
| 131 | In addition to these methods, lock objects can also be used via the |
| 132 | :keyword:`with` statement, e.g.:: |
| 133 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 134 | import _thread |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 135 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 136 | a_lock = _thread.allocate_lock() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 137 | |
| 138 | with a_lock: |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 139 | print("a_lock is locked while this executes") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 140 | |
| 141 | **Caveats:** |
| 142 | |
| 143 | .. index:: module: signal |
| 144 | |
| 145 | * Threads interact strangely with interrupts: the :exc:`KeyboardInterrupt` |
| 146 | exception will be received by an arbitrary thread. (When the :mod:`signal` |
| 147 | module is available, interrupts always go to the main thread.) |
| 148 | |
| 149 | * Calling :func:`sys.exit` or raising the :exc:`SystemExit` exception is |
Georg Brandl | a6053b4 | 2009-09-01 08:11:14 +0000 | [diff] [blame] | 150 | equivalent to calling :func:`_thread.exit`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 151 | |
| 152 | * Not all built-in functions that may block waiting for I/O allow other threads |
| 153 | to run. (The most popular ones (:func:`time.sleep`, :meth:`file.read`, |
| 154 | :func:`select.select`) work as expected.) |
| 155 | |
| 156 | * It is not possible to interrupt the :meth:`acquire` method on a lock --- the |
| 157 | :exc:`KeyboardInterrupt` exception will happen after the lock has been acquired. |
| 158 | |
| 159 | .. index:: pair: threads; IRIX |
| 160 | |
| 161 | * When the main thread exits, it is system defined whether the other threads |
| 162 | survive. On SGI IRIX using the native thread implementation, they survive. On |
| 163 | most other systems, they are killed without executing :keyword:`try` ... |
| 164 | :keyword:`finally` clauses or executing object destructors. |
| 165 | |
| 166 | * When the main thread exits, it does not do any of its usual cleanup (except |
| 167 | that :keyword:`try` ... :keyword:`finally` clauses are honored), and the |
| 168 | standard I/O files are not flushed. |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 169 | |