17db96d56Sopenharmony_ci:mod:`_thread` --- Low-level threading API 27db96d56Sopenharmony_ci========================================== 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: _thread 57db96d56Sopenharmony_ci :synopsis: Low-level threading API. 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci.. index:: 87db96d56Sopenharmony_ci single: light-weight processes 97db96d56Sopenharmony_ci single: processes, light-weight 107db96d56Sopenharmony_ci single: binary semaphores 117db96d56Sopenharmony_ci single: semaphores, binary 127db96d56Sopenharmony_ci 137db96d56Sopenharmony_ci-------------- 147db96d56Sopenharmony_ci 157db96d56Sopenharmony_ciThis module provides low-level primitives for working with multiple threads 167db96d56Sopenharmony_ci(also called :dfn:`light-weight processes` or :dfn:`tasks`) --- multiple threads of 177db96d56Sopenharmony_cicontrol sharing their global data space. For synchronization, simple locks 187db96d56Sopenharmony_ci(also called :dfn:`mutexes` or :dfn:`binary semaphores`) are provided. 197db96d56Sopenharmony_ciThe :mod:`threading` module provides an easier to use and higher-level 207db96d56Sopenharmony_cithreading API built on top of this module. 217db96d56Sopenharmony_ci 227db96d56Sopenharmony_ci.. index:: 237db96d56Sopenharmony_ci single: pthreads 247db96d56Sopenharmony_ci pair: threads; POSIX 257db96d56Sopenharmony_ci 267db96d56Sopenharmony_ci.. versionchanged:: 3.7 277db96d56Sopenharmony_ci This module used to be optional, it is now always available. 287db96d56Sopenharmony_ci 297db96d56Sopenharmony_ciThis module defines the following constants and functions: 307db96d56Sopenharmony_ci 317db96d56Sopenharmony_ci.. exception:: error 327db96d56Sopenharmony_ci 337db96d56Sopenharmony_ci Raised on thread-specific errors. 347db96d56Sopenharmony_ci 357db96d56Sopenharmony_ci .. versionchanged:: 3.3 367db96d56Sopenharmony_ci This is now a synonym of the built-in :exc:`RuntimeError`. 377db96d56Sopenharmony_ci 387db96d56Sopenharmony_ci 397db96d56Sopenharmony_ci.. data:: LockType 407db96d56Sopenharmony_ci 417db96d56Sopenharmony_ci This is the type of lock objects. 427db96d56Sopenharmony_ci 437db96d56Sopenharmony_ci 447db96d56Sopenharmony_ci.. function:: start_new_thread(function, args[, kwargs]) 457db96d56Sopenharmony_ci 467db96d56Sopenharmony_ci Start a new thread and return its identifier. The thread executes the 477db96d56Sopenharmony_ci function *function* with the argument list *args* (which must be a tuple). 487db96d56Sopenharmony_ci The optional *kwargs* argument specifies a dictionary of keyword arguments. 497db96d56Sopenharmony_ci 507db96d56Sopenharmony_ci When the function returns, the thread silently exits. 517db96d56Sopenharmony_ci 527db96d56Sopenharmony_ci When the function terminates with an unhandled exception, 537db96d56Sopenharmony_ci :func:`sys.unraisablehook` is called to handle the exception. The *object* 547db96d56Sopenharmony_ci attribute of the hook argument is *function*. By default, a stack trace is 557db96d56Sopenharmony_ci printed and then the thread exits (but other threads continue to run). 567db96d56Sopenharmony_ci 577db96d56Sopenharmony_ci When the function raises a :exc:`SystemExit` exception, it is silently 587db96d56Sopenharmony_ci ignored. 597db96d56Sopenharmony_ci 607db96d56Sopenharmony_ci .. versionchanged:: 3.8 617db96d56Sopenharmony_ci :func:`sys.unraisablehook` is now used to handle unhandled exceptions. 627db96d56Sopenharmony_ci 637db96d56Sopenharmony_ci 647db96d56Sopenharmony_ci.. function:: interrupt_main(signum=signal.SIGINT, /) 657db96d56Sopenharmony_ci 667db96d56Sopenharmony_ci Simulate the effect of a signal arriving in the main thread. 677db96d56Sopenharmony_ci A thread can use this function to interrupt the main thread, though 687db96d56Sopenharmony_ci there is no guarantee that the interruption will happen immediately. 697db96d56Sopenharmony_ci 707db96d56Sopenharmony_ci If given, *signum* is the number of the signal to simulate. 717db96d56Sopenharmony_ci If *signum* is not given, :data:`signal.SIGINT` is simulated. 727db96d56Sopenharmony_ci 737db96d56Sopenharmony_ci If the given signal isn't handled by Python (it was set to 747db96d56Sopenharmony_ci :data:`signal.SIG_DFL` or :data:`signal.SIG_IGN`), this function does 757db96d56Sopenharmony_ci nothing. 767db96d56Sopenharmony_ci 777db96d56Sopenharmony_ci .. versionchanged:: 3.10 787db96d56Sopenharmony_ci The *signum* argument is added to customize the signal number. 797db96d56Sopenharmony_ci 807db96d56Sopenharmony_ci .. note:: 817db96d56Sopenharmony_ci This does not emit the corresponding signal but schedules a call to 827db96d56Sopenharmony_ci the associated handler (if it exists). 837db96d56Sopenharmony_ci If you want to truly emit the signal, use :func:`signal.raise_signal`. 847db96d56Sopenharmony_ci 857db96d56Sopenharmony_ci 867db96d56Sopenharmony_ci.. function:: exit() 877db96d56Sopenharmony_ci 887db96d56Sopenharmony_ci Raise the :exc:`SystemExit` exception. When not caught, this will cause the 897db96d56Sopenharmony_ci thread to exit silently. 907db96d56Sopenharmony_ci 917db96d56Sopenharmony_ci.. 927db96d56Sopenharmony_ci function:: exit_prog(status) 937db96d56Sopenharmony_ci 947db96d56Sopenharmony_ci Exit all threads and report the value of the integer argument 957db96d56Sopenharmony_ci *status* as the exit status of the entire program. 967db96d56Sopenharmony_ci **Caveat:** code in pending :keyword:`finally` clauses, in this thread 977db96d56Sopenharmony_ci or in other threads, is not executed. 987db96d56Sopenharmony_ci 997db96d56Sopenharmony_ci 1007db96d56Sopenharmony_ci.. function:: allocate_lock() 1017db96d56Sopenharmony_ci 1027db96d56Sopenharmony_ci Return a new lock object. Methods of locks are described below. The lock is 1037db96d56Sopenharmony_ci initially unlocked. 1047db96d56Sopenharmony_ci 1057db96d56Sopenharmony_ci 1067db96d56Sopenharmony_ci.. function:: get_ident() 1077db96d56Sopenharmony_ci 1087db96d56Sopenharmony_ci Return the 'thread identifier' of the current thread. This is a nonzero 1097db96d56Sopenharmony_ci integer. Its value has no direct meaning; it is intended as a magic cookie to 1107db96d56Sopenharmony_ci be used e.g. to index a dictionary of thread-specific data. Thread identifiers 1117db96d56Sopenharmony_ci may be recycled when a thread exits and another thread is created. 1127db96d56Sopenharmony_ci 1137db96d56Sopenharmony_ci 1147db96d56Sopenharmony_ci.. function:: get_native_id() 1157db96d56Sopenharmony_ci 1167db96d56Sopenharmony_ci Return the native integral Thread ID of the current thread assigned by the kernel. 1177db96d56Sopenharmony_ci This is a non-negative integer. 1187db96d56Sopenharmony_ci Its value may be used to uniquely identify this particular thread system-wide 1197db96d56Sopenharmony_ci (until the thread terminates, after which the value may be recycled by the OS). 1207db96d56Sopenharmony_ci 1217db96d56Sopenharmony_ci .. availability:: Windows, FreeBSD, Linux, macOS, OpenBSD, NetBSD, AIX. 1227db96d56Sopenharmony_ci 1237db96d56Sopenharmony_ci .. versionadded:: 3.8 1247db96d56Sopenharmony_ci 1257db96d56Sopenharmony_ci 1267db96d56Sopenharmony_ci.. function:: stack_size([size]) 1277db96d56Sopenharmony_ci 1287db96d56Sopenharmony_ci Return the thread stack size used when creating new threads. The optional 1297db96d56Sopenharmony_ci *size* argument specifies the stack size to be used for subsequently created 1307db96d56Sopenharmony_ci threads, and must be 0 (use platform or configured default) or a positive 1317db96d56Sopenharmony_ci integer value of at least 32,768 (32 KiB). If *size* is not specified, 1327db96d56Sopenharmony_ci 0 is used. If changing the thread stack size is 1337db96d56Sopenharmony_ci unsupported, a :exc:`RuntimeError` is raised. If the specified stack size is 1347db96d56Sopenharmony_ci invalid, a :exc:`ValueError` is raised and the stack size is unmodified. 32 KiB 1357db96d56Sopenharmony_ci is currently the minimum supported stack size value to guarantee sufficient 1367db96d56Sopenharmony_ci stack space for the interpreter itself. Note that some platforms may have 1377db96d56Sopenharmony_ci particular restrictions on values for the stack size, such as requiring a 1387db96d56Sopenharmony_ci minimum stack size > 32 KiB or requiring allocation in multiples of the system 1397db96d56Sopenharmony_ci memory page size - platform documentation should be referred to for more 1407db96d56Sopenharmony_ci information (4 KiB pages are common; using multiples of 4096 for the stack size is 1417db96d56Sopenharmony_ci the suggested approach in the absence of more specific information). 1427db96d56Sopenharmony_ci 1437db96d56Sopenharmony_ci .. availability:: Windows, pthreads. 1447db96d56Sopenharmony_ci 1457db96d56Sopenharmony_ci Unix platforms with POSIX threads support. 1467db96d56Sopenharmony_ci 1477db96d56Sopenharmony_ci 1487db96d56Sopenharmony_ci.. data:: TIMEOUT_MAX 1497db96d56Sopenharmony_ci 1507db96d56Sopenharmony_ci The maximum value allowed for the *timeout* parameter of 1517db96d56Sopenharmony_ci :meth:`Lock.acquire`. Specifying a timeout greater than this value will 1527db96d56Sopenharmony_ci raise an :exc:`OverflowError`. 1537db96d56Sopenharmony_ci 1547db96d56Sopenharmony_ci .. versionadded:: 3.2 1557db96d56Sopenharmony_ci 1567db96d56Sopenharmony_ci 1577db96d56Sopenharmony_ciLock objects have the following methods: 1587db96d56Sopenharmony_ci 1597db96d56Sopenharmony_ci 1607db96d56Sopenharmony_ci.. method:: lock.acquire(blocking=True, timeout=-1) 1617db96d56Sopenharmony_ci 1627db96d56Sopenharmony_ci Without any optional argument, this method acquires the lock unconditionally, if 1637db96d56Sopenharmony_ci necessary waiting until it is released by another thread (only one thread at a 1647db96d56Sopenharmony_ci time can acquire a lock --- that's their reason for existence). 1657db96d56Sopenharmony_ci 1667db96d56Sopenharmony_ci If the *blocking* argument is present, the action depends on its 1677db96d56Sopenharmony_ci value: if it is False, the lock is only acquired if it can be acquired 1687db96d56Sopenharmony_ci immediately without waiting, while if it is True, the lock is acquired 1697db96d56Sopenharmony_ci unconditionally as above. 1707db96d56Sopenharmony_ci 1717db96d56Sopenharmony_ci If the floating-point *timeout* argument is present and positive, it 1727db96d56Sopenharmony_ci specifies the maximum wait time in seconds before returning. A negative 1737db96d56Sopenharmony_ci *timeout* argument specifies an unbounded wait. You cannot specify 1747db96d56Sopenharmony_ci a *timeout* if *blocking* is False. 1757db96d56Sopenharmony_ci 1767db96d56Sopenharmony_ci The return value is ``True`` if the lock is acquired successfully, 1777db96d56Sopenharmony_ci ``False`` if not. 1787db96d56Sopenharmony_ci 1797db96d56Sopenharmony_ci .. versionchanged:: 3.2 1807db96d56Sopenharmony_ci The *timeout* parameter is new. 1817db96d56Sopenharmony_ci 1827db96d56Sopenharmony_ci .. versionchanged:: 3.2 1837db96d56Sopenharmony_ci Lock acquires can now be interrupted by signals on POSIX. 1847db96d56Sopenharmony_ci 1857db96d56Sopenharmony_ci 1867db96d56Sopenharmony_ci.. method:: lock.release() 1877db96d56Sopenharmony_ci 1887db96d56Sopenharmony_ci Releases the lock. The lock must have been acquired earlier, but not 1897db96d56Sopenharmony_ci necessarily by the same thread. 1907db96d56Sopenharmony_ci 1917db96d56Sopenharmony_ci 1927db96d56Sopenharmony_ci.. method:: lock.locked() 1937db96d56Sopenharmony_ci 1947db96d56Sopenharmony_ci Return the status of the lock: ``True`` if it has been acquired by some thread, 1957db96d56Sopenharmony_ci ``False`` if not. 1967db96d56Sopenharmony_ci 1977db96d56Sopenharmony_ciIn addition to these methods, lock objects can also be used via the 1987db96d56Sopenharmony_ci:keyword:`with` statement, e.g.:: 1997db96d56Sopenharmony_ci 2007db96d56Sopenharmony_ci import _thread 2017db96d56Sopenharmony_ci 2027db96d56Sopenharmony_ci a_lock = _thread.allocate_lock() 2037db96d56Sopenharmony_ci 2047db96d56Sopenharmony_ci with a_lock: 2057db96d56Sopenharmony_ci print("a_lock is locked while this executes") 2067db96d56Sopenharmony_ci 2077db96d56Sopenharmony_ci**Caveats:** 2087db96d56Sopenharmony_ci 2097db96d56Sopenharmony_ci .. index:: pair: module; signal 2107db96d56Sopenharmony_ci 2117db96d56Sopenharmony_ci* Threads interact strangely with interrupts: the :exc:`KeyboardInterrupt` 2127db96d56Sopenharmony_ci exception will be received by an arbitrary thread. (When the :mod:`signal` 2137db96d56Sopenharmony_ci module is available, interrupts always go to the main thread.) 2147db96d56Sopenharmony_ci 2157db96d56Sopenharmony_ci* Calling :func:`sys.exit` or raising the :exc:`SystemExit` exception is 2167db96d56Sopenharmony_ci equivalent to calling :func:`_thread.exit`. 2177db96d56Sopenharmony_ci 2187db96d56Sopenharmony_ci* It is not possible to interrupt the :meth:`acquire` method on a lock --- the 2197db96d56Sopenharmony_ci :exc:`KeyboardInterrupt` exception will happen after the lock has been acquired. 2207db96d56Sopenharmony_ci 2217db96d56Sopenharmony_ci* When the main thread exits, it is system defined whether the other threads 2227db96d56Sopenharmony_ci survive. On most systems, they are killed without executing 2237db96d56Sopenharmony_ci :keyword:`try` ... :keyword:`finally` clauses or executing object 2247db96d56Sopenharmony_ci destructors. 2257db96d56Sopenharmony_ci 2267db96d56Sopenharmony_ci* When the main thread exits, it does not do any of its usual cleanup (except 2277db96d56Sopenharmony_ci that :keyword:`try` ... :keyword:`finally` clauses are honored), and the 2287db96d56Sopenharmony_ci standard I/O files are not flushed. 2297db96d56Sopenharmony_ci 230