Skip to content

Commit cf62581

Browse files
Doc: Update free-threading how-to per review
1 parent b85e10f commit cf62581

File tree

1 file changed

+21
-34
lines changed

1 file changed

+21
-34
lines changed

Doc/howto/free-threading-python.rst

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ Python support for free threading
66

77
Starting with the 3.13 release, CPython has support for a build of
88
Python called :term:`free threading` where the :term:`global interpreter lock`
9-
(GIL) is disabled. Free-threaded execution allows for full utilization of the
9+
(GIL) is disabled. Free-threaded execution allows for full utilization of the
1010
available processing power by running threads in parallel on available CPU cores.
1111
While not all software will benefit from this automatically, programs
1212
designed with threading in mind will run faster on multi-core hardware.
1313

14-
The free-threaded mode is working and continues to be improved, but
15-
there is some additional overhead in single-threaded workloads compared
16-
to the regular build. Additionally, third-party packages, in particular ones
14+
The free-threaded mode continues to be improved with each release.
15+
Additionally, third-party packages, in particular ones
1716
with an :term:`extension module`, may not be ready for use in a
1817
free-threaded build, and will re-enable the :term:`GIL`.
1918

@@ -101,60 +100,48 @@ This section describes known limitations of the free-threaded CPython build.
101100
Immortalization
102101
---------------
103102

104-
The free-threaded build of the 3.13 release makes some objects :term:`immortal`.
103+
In the free-threaded build, some objects are :term:`immortal`.
105104
Immortal objects are not deallocated and have reference counts that are
106-
never modified. This is done to avoid reference count contention that would
105+
never modified.  This is done to avoid reference count contention that would
107106
prevent efficient multi-threaded scaling.
108107

109-
An object will be made immortal when a new thread is started for the first time
110-
after the main thread is running. The following objects are immortalized:
108+
As of the 3.14 release, immortalization is limited to:
111109

112-
* :ref:`function <user-defined-funcs>` objects declared at the module level
113-
* :ref:`method <instance-methods>` descriptors
114-
* :ref:`code <code-objects>` objects
115-
* :term:`module` objects and their dictionaries
116-
* :ref:`classes <classes>` (type objects)
117-
118-
Because immortal objects are never deallocated, applications that create many
119-
objects of these types may see increased memory usage. This is expected to be
120-
addressed in the 3.14 release.
121-
122-
Additionally, numeric and string literals in the code as well as strings
123-
returned by :func:`sys.intern` are also immortalized. This behavior is
124-
expected to remain in the 3.14 free-threaded build.
110+
* Code constants: numeric literals, string literals, and tuple literals
111+
composed of other constants.
112+
* Strings returned by :func:`sys.intern`.
125113

126114

127115
Frame objects
128116
-------------
129117

130118
It is not safe to access :ref:`frame <frame-objects>` objects from other
131-
threads and doing so may cause your program to crash . This means that
119+
threads. This means that
132120
:func:`sys._current_frames` is generally not safe to use in a free-threaded
133-
build. Functions like :func:`inspect.currentframe` and :func:`sys._getframe`
121+
build. Functions like :func:`inspect.currentframe` and :func:`sys._getframe`
134122
are generally safe as long as the resulting frame object is not passed to
135123
another thread.
136124

137125
Iterators
138126
---------
139127

140-
Sharing the same iterator object between multiple threads is generally not
141-
safe and threads may see duplicate or missing elements when iterating or crash
142-
the interpreter.
128+
While sharing the same iterator object between multiple threads is generally not
129+
safe from a logical perspective (threads may see duplicate or missing elements
130+
when iterating), the 3.14 free-threaded build prevents interpreter crashes
131+
that occurred in earlier versions.
143132

144133

145134
Single-threaded performance
146135
---------------------------
147136

148137
The free-threaded build has additional overhead when executing Python code
149-
compared to the default GIL-enabled build. In 3.13, this overhead is about
150-
40% on the `pyperformance <https://pyperformance.readthedocs.io/>`_ suite.
138+
compared to the default GIL-enabled build. This overhead was reduced
139+
in the 3.14 release (which re-enabled the specializing adaptive
140+
interpreter, :pep:`659`). Reducing this overhead further remains an
141+
active development goal, with an aim for 10% or less on the
142+
`pyperformance <https://pyperformance.readthedocs.io/>`_ suite compared to the default GIL-enabled build.
151143
Programs that spend most of their time in C extensions or I/O will see
152-
less of an impact. The largest impact is because the specializing adaptive
153-
interpreter (:pep:`659`) is disabled in the free-threaded build. We expect
154-
to re-enable it in a thread-safe way in the 3.14 release. This overhead is
155-
expected to be reduced in upcoming Python release. We are aiming for an
156-
overhead of 10% or less on the pyperformance suite compared to the default
157-
GIL-enabled build.
144+
less of an impact.
158145

159146

160147
Behavioral changes

0 commit comments

Comments
 (0)