← All notes

Python _thread: the low-level threading API under threading

Answer: The _thread module is a low-level threading API. The threading module wraps it at a higher level.

Threads

The module offers primitives for multiple threads of control that share a global data space, also called light-weight processes or tasks, and simple locks, also called mutexes or binary semaphores.

The module is always available from version 3.7.

error is raised on thread-specific errors and is a synonym of RuntimeError from version 3.3.

start_new_thread(function, args[, kwargs]) starts a new thread and returns its identifier. args must be a tuple. When the function returns, the thread exits silently. An unhandled exception is passed to sys.unraisablehook, and SystemExit from the function is ignored.

interrupt_main(signum=signal.SIGINT) simulates a signal arriving in the main thread, with no guarantee of an immediate interruption. If the signal is SIG_DFL or SIG_IGN, the function does nothing. The function schedules the associated handler. If you want to truly emit the signal, use signal.raise_signal.

exit() raises SystemExit. When not caught, the thread exits silently. get_ident() returns the current thread identifier, a nonzero integer. get_native_id() returns the native Thread ID assigned by the kernel, added in 3.8.

stack_size([size]) reports, or changes, the stack size used for new threads. 0 means the platform default, and a positive value must be at least 32,768 (32 KiB). Unsupported or invalid sizes raise RuntimeError or ValueError.

Locks

allocate_lock() returns a new unlocked lock. LockType is the type of lock objects.

acquire(blocking=True, timeout=-1) waits for the lock when blocking is true. When blocking is false, it takes the lock only if that can be done immediately. TIMEOUT_MAX is the maximum value allowed for the timeout parameter. A greater timeout raises OverflowError.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, _thread — Low-level threading API; CPython, _thread documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, _thread — Low-level threading API; CPython, _thread documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.

Cite this

Permanent ID: iil:note:2qrfn9hw

iinventedlight. 2026. "Python _thread: the low-level threading API under threading." iinventedlight Library, iil:note:2qrfn9hw, version of 2026-11-27. https://iinventedlight.com/library/notes/python-thread/. License: CC-BY-4.0.

@misc{iil_note_2qrfn9hw,
  author = {{iinventedlight}},
  title = {{Python \_thread: the low-level threading API under threading}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-27},
  url = {https://iinventedlight.com/library/notes/python-thread/},
  note = {Permanent ID iil:note:2qrfn9hw. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-thread.md.