Skip to main content

threading

Higher-level threading API built on _thread. Provides Thread, synchronisation primitives, thread-local data, and timers.

Source-of-record: Lib/threading.py, threading docs.

Module-level

NameReturns
active_count()Live thread count.
current_thread()Calling Thread.
main_thread()Main Thread.
enumerate()List of live threads.
get_ident() / get_native_id()Identifier.
excepthook(args)Override unhandled exception hook.
settrace(func) / setprofile(func)Install tracers.
stack_size([size])New-thread stack size.
local()Thread-local storage.

Thread

APIPurpose
Thread(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)Construct.
start() / run()Launch / body.
join(timeout=None)Wait.
is_alive()Predicate.
name, ident, native_id, daemonAttributes.

Synchronisation

PrimitiveMethods
Lockacquire(blocking=True, timeout=-1), release(), locked().
RLockRe-entrant lock.
Condition(lock=None)wait, wait_for, notify, notify_all.
Semaphore(value=1) / BoundedSemaphoreacquire, release.
Eventset, clear, wait, is_set.
Barrier(parties, action=None, timeout=None)wait, reset, abort.
Timer(interval, function, args=None, kwargs=None)start, cancel.

All locks act as context managers via with.

GIL

CPython holds a global interpreter lock; threading runs cooperatively in CPU-bound Python code. 3.13 introduced an opt-in free-threaded build; 3.14 stabilises it.

Gopy status

AreaState
Thread, daemon, joinComplete.
Locks (Lock, RLock, Semaphore, BoundedSemaphore)Complete.
Condition, Event, BarrierComplete.
Timer, localComplete.
Free-threaded interpreterOut of scope; gopy is single-threaded VM.

Reference

  • CPython 3.14: threading.
  • Lib/threading.py.
  • module/threading/. gopy port.