|
4 | 4 | .. module:: _thread |
5 | 5 | :synopsis: multithreading support |
6 | 6 |
|
7 | | -|see_cpython_module| :mod:`python:_thread`. |
| 7 | +This module provides low-level primitives for working with multiple threads |
| 8 | +of execution: spawning new threads, querying the current thread identifier, |
| 9 | +and creating mutex locks for synchronisation. It is intended as a foundation |
| 10 | +for higher-level threading abstractions. |
8 | 11 |
|
9 | | -This module implements multithreading support. |
| 12 | +A thread, once started, executes independently from the thread that spawned it. |
| 13 | +There is no built-in way to join, cancel, or otherwise interact with a running |
| 14 | +thread; synchronisation must be performed explicitly using lock objects. |
10 | 15 |
|
11 | | -This module is highly experimental and its API is not yet fully settled |
12 | | -and not yet described in this documentation. |
| 16 | +Functions |
| 17 | +--------- |
| 18 | + |
| 19 | +.. function:: start_new_thread(function: Callable[..., Any], args: tuple, kwargs: dict | None = None) -> int |
| 20 | + |
| 21 | + Start a new thread that runs ``function(*args, **kwargs)`` and return its |
| 22 | + integer thread identifier. *args* must be a tuple of positional arguments; |
| 23 | + *kwargs*, if given, must be a dict of keyword arguments. |
| 24 | + |
| 25 | + If the thread terminates with an unhandled exception (other than |
| 26 | + :exc:`SystemExit`), a traceback is printed and the thread exits. |
| 27 | + |
| 28 | +.. function:: exit() -> None |
| 29 | + |
| 30 | + Raise :exc:`SystemExit` in the calling thread, terminating it. This has |
| 31 | + the same effect as ``raise SystemExit``. |
| 32 | + |
| 33 | +.. function:: get_ident() -> int |
| 34 | + |
| 35 | + Return the integer "thread identifier" of the current thread. The value |
| 36 | + has no direct meaning beyond identifying a particular thread for the |
| 37 | + lifetime of that thread. |
| 38 | + |
| 39 | +.. function:: allocate_lock() -> Lock |
| 40 | + |
| 41 | + Return a new lock object (initially unlocked). See the :class:`Lock` |
| 42 | + class below for details. |
| 43 | + |
| 44 | +.. function:: stack_size(size: int = 0, /) -> int |
| 45 | + |
| 46 | + Return the thread stack size used when creating new threads. The optional |
| 47 | + *size* argument sets the stack size, in bytes, to be used for subsequently |
| 48 | + created threads; a value of ``0`` selects the platform default. The |
| 49 | + previous value is returned. |
| 50 | + |
| 51 | +Lock objects |
| 52 | +------------ |
| 53 | + |
| 54 | +.. class:: Lock |
| 55 | + |
| 56 | + A primitive mutex (mutual exclusion) lock. When locked, it belongs to |
| 57 | + the thread that locked it; when unlocked, it does not belong to any |
| 58 | + thread. Lock objects are created via :func:`allocate_lock` and cannot |
| 59 | + be instantiated directly. |
| 60 | + |
| 61 | + Lock objects support the context manager protocol: using a lock with the |
| 62 | + ``with`` statement acquires it on entry and releases it on exit. |
| 63 | + |
| 64 | + .. method:: acquire(blocking: bool = True, /) -> bool |
| 65 | + |
| 66 | + Acquire the lock. If *blocking* is true (the default), block until |
| 67 | + the lock is available, then take it and return ``True``. If |
| 68 | + *blocking* is false, return ``True`` immediately if the lock could |
| 69 | + be taken, or ``False`` if it could not. |
| 70 | + |
| 71 | + .. method:: release() -> None |
| 72 | + |
| 73 | + Release the lock, allowing another thread that is blocked waiting |
| 74 | + for it to proceed. The lock must be held by the calling thread; |
| 75 | + calling :meth:`release` on an unlocked lock raises :exc:`RuntimeError`. |
| 76 | + |
| 77 | + .. method:: locked() -> bool |
| 78 | + |
| 79 | + Return ``True`` if the lock is currently held by some thread, and |
| 80 | + ``False`` otherwise. |
0 commit comments