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 | |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 31 | It defines the following constants and functions: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
| 33 | |
| 34 | .. exception:: error |
| 35 | |
| 36 | Raised on thread-specific errors. |
| 37 | |
Antoine Pitrou | fcf81fd | 2011-02-28 22:03:34 +0000 | [diff] [blame] | 38 | .. versionchanged:: 3.3 |
| 39 | This is now a synonym of the built-in :exc:`RuntimeError`. |
| 40 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 41 | |
| 42 | .. data:: LockType |
| 43 | |
| 44 | This is the type of lock objects. |
| 45 | |
| 46 | |
| 47 | .. function:: start_new_thread(function, args[, kwargs]) |
| 48 | |
| 49 | Start a new thread and return its identifier. The thread executes the function |
| 50 | *function* with the argument list *args* (which must be a tuple). The optional |
| 51 | *kwargs* argument specifies a dictionary of keyword arguments. When the function |
| 52 | returns, the thread silently exits. When the function terminates with an |
| 53 | unhandled exception, a stack trace is printed and then the thread exits (but |
| 54 | other threads continue to run). |
| 55 | |
| 56 | |
| 57 | .. function:: interrupt_main() |
| 58 | |
| 59 | Raise a :exc:`KeyboardInterrupt` exception in the main thread. A subthread can |
| 60 | use this function to interrupt the main thread. |
| 61 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 62 | |
| 63 | .. function:: exit() |
| 64 | |
| 65 | Raise the :exc:`SystemExit` exception. When not caught, this will cause the |
| 66 | thread to exit silently. |
| 67 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 68 | .. |
| 69 | function:: exit_prog(status) |
| 70 | |
| 71 | Exit all threads and report the value of the integer argument |
| 72 | *status* as the exit status of the entire program. |
| 73 | **Caveat:** code in pending :keyword:`finally` clauses, in this thread |
| 74 | or in other threads, is not executed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | .. function:: allocate_lock() |
| 78 | |
| 79 | Return a new lock object. Methods of locks are described below. The lock is |
| 80 | initially unlocked. |
| 81 | |
| 82 | |
| 83 | .. function:: get_ident() |
| 84 | |
| 85 | Return the 'thread identifier' of the current thread. This is a nonzero |
| 86 | integer. Its value has no direct meaning; it is intended as a magic cookie to |
| 87 | be used e.g. to index a dictionary of thread-specific data. Thread identifiers |
| 88 | may be recycled when a thread exits and another thread is created. |
| 89 | |
| 90 | |
| 91 | .. function:: stack_size([size]) |
| 92 | |
| 93 | Return the thread stack size used when creating new threads. The optional |
| 94 | *size* argument specifies the stack size to be used for subsequently created |
| 95 | threads, and must be 0 (use platform or configured default) or a positive |
Martin Panter | 31e7f50 | 2015-08-31 03:15:52 +0000 | [diff] [blame] | 96 | integer value of at least 32,768 (32 KiB). If *size* is not specified, |
| 97 | 0 is used. If changing the thread stack size is |
Georg Brandl | 9a13b43 | 2012-04-05 09:53:04 +0200 | [diff] [blame] | 98 | unsupported, a :exc:`RuntimeError` is raised. If the specified stack size is |
Serhiy Storchaka | f8def28 | 2013-02-16 17:29:56 +0200 | [diff] [blame] | 99 | invalid, a :exc:`ValueError` is raised and the stack size is unmodified. 32 KiB |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | is currently the minimum supported stack size value to guarantee sufficient |
| 101 | stack space for the interpreter itself. Note that some platforms may have |
| 102 | particular restrictions on values for the stack size, such as requiring a |
Serhiy Storchaka | f8def28 | 2013-02-16 17:29:56 +0200 | [diff] [blame] | 103 | minimum stack size > 32 KiB or requiring allocation in multiples of the system |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 | memory page size - platform documentation should be referred to for more |
Serhiy Storchaka | f8def28 | 2013-02-16 17:29:56 +0200 | [diff] [blame] | 105 | information (4 KiB pages are common; using multiples of 4096 for the stack size is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 106 | the suggested approach in the absence of more specific information). |
| 107 | Availability: Windows, systems with POSIX threads. |
| 108 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 110 | .. data:: TIMEOUT_MAX |
| 111 | |
| 112 | The maximum value allowed for the *timeout* parameter of |
Georg Brandl | 6faee4e | 2010-09-21 14:48:28 +0000 | [diff] [blame] | 113 | :meth:`Lock.acquire`. Specifying a timeout greater than this value will |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 114 | raise an :exc:`OverflowError`. |
| 115 | |
Antoine Pitrou | adbc009 | 2010-04-19 14:05:51 +0000 | [diff] [blame] | 116 | .. versionadded:: 3.2 |
| 117 | |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 118 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 | Lock objects have the following methods: |
| 120 | |
| 121 | |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 122 | .. method:: lock.acquire(waitflag=1, timeout=-1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 123 | |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 124 | Without any optional argument, this method acquires the lock unconditionally, if |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 125 | necessary waiting until it is released by another thread (only one thread at a |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 126 | time can acquire a lock --- that's their reason for existence). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 128 | If the integer *waitflag* argument is present, the action depends on its |
| 129 | value: if it is zero, the lock is only acquired if it can be acquired |
| 130 | immediately without waiting, while if it is nonzero, the lock is acquired |
| 131 | unconditionally as above. |
| 132 | |
| 133 | If the floating-point *timeout* argument is present and positive, it |
| 134 | specifies the maximum wait time in seconds before returning. A negative |
| 135 | *timeout* argument specifies an unbounded wait. You cannot specify |
| 136 | a *timeout* if *waitflag* is zero. |
| 137 | |
| 138 | The return value is ``True`` if the lock is acquired successfully, |
| 139 | ``False`` if not. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 140 | |
Antoine Pitrou | adbc009 | 2010-04-19 14:05:51 +0000 | [diff] [blame] | 141 | .. versionchanged:: 3.2 |
| 142 | The *timeout* parameter is new. |
| 143 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 144 | .. versionchanged:: 3.2 |
| 145 | Lock acquires can now be interrupted by signals on POSIX. |
| 146 | |
| 147 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 148 | .. method:: lock.release() |
| 149 | |
| 150 | Releases the lock. The lock must have been acquired earlier, but not |
| 151 | necessarily by the same thread. |
| 152 | |
| 153 | |
| 154 | .. method:: lock.locked() |
| 155 | |
| 156 | Return the status of the lock: ``True`` if it has been acquired by some thread, |
| 157 | ``False`` if not. |
| 158 | |
| 159 | In addition to these methods, lock objects can also be used via the |
| 160 | :keyword:`with` statement, e.g.:: |
| 161 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 162 | import _thread |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 163 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 164 | a_lock = _thread.allocate_lock() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 165 | |
| 166 | with a_lock: |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 167 | print("a_lock is locked while this executes") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 168 | |
| 169 | **Caveats:** |
| 170 | |
| 171 | .. index:: module: signal |
| 172 | |
| 173 | * Threads interact strangely with interrupts: the :exc:`KeyboardInterrupt` |
| 174 | exception will be received by an arbitrary thread. (When the :mod:`signal` |
| 175 | module is available, interrupts always go to the main thread.) |
| 176 | |
| 177 | * Calling :func:`sys.exit` or raising the :exc:`SystemExit` exception is |
Georg Brandl | a6053b4 | 2009-09-01 08:11:14 +0000 | [diff] [blame] | 178 | equivalent to calling :func:`_thread.exit`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 179 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 180 | * It is not possible to interrupt the :meth:`acquire` method on a lock --- the |
| 181 | :exc:`KeyboardInterrupt` exception will happen after the lock has been acquired. |
| 182 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 183 | * When the main thread exits, it is system defined whether the other threads |
Antoine Pitrou | e4754bd | 2010-04-19 14:09:57 +0000 | [diff] [blame] | 184 | survive. On most systems, they are killed without executing |
| 185 | :keyword:`try` ... :keyword:`finally` clauses or executing object |
| 186 | destructors. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 187 | |
| 188 | * When the main thread exits, it does not do any of its usual cleanup (except |
| 189 | that :keyword:`try` ... :keyword:`finally` clauses are honored), and the |
| 190 | standard I/O files are not flushed. |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 191 | |