GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing docs.python.org — Python support for free threading Python support for free threading¶Starting with the 3.13 release, CPython has support for a build of Python called free threading where the global interpreter lock (GIL) is disabled. Free-threaded execution allows for full utilization of the available processing power by running threads in parallel on available CPU cores. While not all software will benefit from this automatically, programs designed with threading in mind will run faster on multi-core hardware. Some third-party packages, in particular ones with an extension module, may not be ready for use in a free-threaded build, and will re-enable the GIL. This document describes the implications of free threading for Python code. See C API Extension Support for Free Threading for information on how to write C extensions that support the free-threaded build. See also PEP 703 – Making the Global Interpreter Lock Optional in CPython for an overall description of free-threaded Python. Installation¶Starting with Python 3.13, the official macOS and Windows installers optionally support installing free-threaded Python binaries. The installers are available at https://www.python.org/downloads/. For information on other platforms, see the Installing a Free-Threaded Python, a community-maintained installation guide for installing free-threaded Python. When building CPython from source, the Identifying free-threaded Python¶To check if the current interpreter supports free-threading, The The global interpreter lock in free-threaded Python¶Free-threaded builds of CPython support optionally running with the GIL enabled
at runtime using the environment variable The GIL may also automatically be enabled when importing a C-API extension module that is not explicitly marked as supporting free threading. A warning will be printed in this case. In addition to individual package documentation, the following websites track the status of popular packages support for free threading: Thread safety¶The free-threaded build of CPython aims to provide similar thread-safety
behavior at the Python level to the default GIL-enabled build. Built-in
types like Note It’s recommended to use the Known limitations¶This section describes known limitations of the free-threaded CPython build. Immortalization¶In the free-threaded build, some objects are immortal. Immortal objects are not deallocated and have reference counts that are never modified. This is done to avoid reference count contention that would prevent efficient multi-threaded scaling. As of the 3.14 release, immortalization is limited to:
Frame objects¶It is not safe to access Iterators¶It is generally not thread-safe to access the same iterator object from multiple threads concurrently, and threads may see duplicate or missing elements. Single-threaded performance¶The free-threaded build has additional overhead when executing Python code compared to the default GIL-enabled build. The amount of overhead depends on the workload and hardware. On the pyperformance benchmark suite, the average overhead ranges from about 1% on macOS aarch64 to 8% on x86-64 Linux systems. Behavioral changes¶This section describes CPython behavioural changes with the free-threaded build. Context variables¶In the free-threaded build, the flag Warning filters¶In the free-threaded build, the flag Increased memory usage¶The free-threaded build will typically use more memory compared to the default build. There are multiple reasons for this, mostly due to design decisions. All interned strings are immortal¶For modern Python versions (since version 2.3), interning a string (e.g. with
Non-GC objects have a larger object header¶The free-threaded build uses a different QSBR can delay freeing of memory¶In order to safely implement lock-free data structures, a safe memory
reclamation (SMR) scheme is used, known as quiescent state-based reclamation
(QSBR). This means that the memory backing data structures allowing lock-free
access will use QSBR, which defers the free operation, rather than immediately
freeing the memory. Two examples of these data structures are the list object
and the dictionary keys object. See mimalloc allocator vs pymalloc¶The default build will normally use the “pymalloc” memory allocator for small allocations (512 bytes or smaller). The free-threaded build does not use pymalloc and allocates all Python objects using the “mimalloc” allocator. The pymalloc allocator has the following properties that help keep memory usage low: small per-allocated-block overhead, effective memory fragmentation prevention, and quick return of free memory to the operating system. The mimalloc allocator does quite well in these respects as well but can have some more overhead. In the free-threaded build, mimalloc manages memory in a number of separate heaps (currently four). For example, all GC supporting objects are allocated from their own heap. Using separate heaps means that free memory in one heap cannot be used for an allocation that uses another heap. Also, some heaps are configured to use QSBR (quiescent-state based reclamation) when freeing the memory that backs up the heap (known as “pages” in mimalloc terminology). The use of QSBR creates a delay between all memory blocks for a page being freed and the memory page being released, either for new allocations or back to the OS. The mimalloc allocator also defers returning freed memory back to the OS. You
can reduce that delay by setting the environment variable
Free-threaded reference counting can cause objects to live longer¶In the default build, when an object’s reference count reaches zero, it is normally deallocated. The free-threaded build uses “biased reference counting”, with a fast-path for objects “owned” by the current thread and a slow path for other objects. See PEP 703 for additional details. Any time an object’s reference count ends up in a “queued” state, deallocation can be deferred. The queued state is cleared from the “eval breaker” section of the bytecode evaluator. The free-threaded build also allows a different mode of reference counting, known as “deferred reference counting”. This mode is enabled by setting a flag on a per-object basis. Deferred reference counting is enabled for the following types:
When deferred reference counting is enabled, references from Python function stacks are not added to the reference count. This scheme reduces the overhead of reference counting, especially for objects used from multiple threads. Because the stack references are not counted, objects with deferred reference counting are not immediately freed when their internal reference count goes to zero. Instead, they are examined by the next GC run and, if no stack references to them are found, they are freed. This means these objects are freed by the GC and not when their reference count goes to zero, as is typical. Per-thread reference counting can delay freeing objects¶To avoid contention on the reference count fields of frequently shared objects, the free-threaded build also uses “per-thread reference counting” for a few selected object types. Rather than updating a single shared reference count, each thread maintains its own local reference count array, indexed by a unique id assigned to the object. The true reference count is only computed by summing the per-thread counts when the object’s local count drops to zero. Per-thread reference counting is currently used for:
Because the per-thread counts must be merged back to the object before it
can be deallocated, objects using per-thread reference counting are
typically freed later than they would be in the default build. In
particular, such an object is usually not freed until the thread that
referenced it reaches a safe point (for example, in the “eval breaker”
section of the bytecode evaluator) or exits. Running
© Copyright 2001 Python Software Foundation.
This page is licensed under the Python Software Foundation License Version 2. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License. See History and License for more information. The Python Software Foundation is a non-profit corporation. Please donate. Last updated on Nov 12, 2026 (08:53 UTC). Found a bug? Created using Sphinx 9.1.1. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |