-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-124370: Add "howto" for free-threaded Python #124371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
5b4a8aa
0252cf5
df1489e
5658fa8
ae0f637
1554fb9
3b173e0
91ec1ca
6078cfd
e02ed00
526737e
f24bdb7
a3ad6b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,119 @@ | ||||||||||
.. _freethreading-python-howto: | ||||||||||
|
||||||||||
********************************* | ||||||||||
Python Support for Free Threading | ||||||||||
********************************* | ||||||||||
|
||||||||||
Starting with the 3.13 release, CPython has experimental support for running | ||||||||||
with the :term:`global interpreter lock` (GIL) disabled in a configuration | ||||||||||
|
||||||||||
called :term:`free threading`. This document describes the implications of | ||||||||||
free threading for Python code. See :ref:`freethreading-extensions-howto` for | ||||||||||
information on how to write C extensions that support the free-threaded build. | ||||||||||
|
||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we could add a quick snapshot of the overall plan: if everything works out, eventually free threading will be the only build, etc. Also, maybe a statement about how most programmer won't need to be concerned with this, we're doing a lot to keep everyday Python programs behaving the same, etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would perhaps add a note in the seealso block that refers to the PEP and adds 1 or 2 highlights:
|
||||||||||
|
||||||||||
Installation | ||||||||||
============ | ||||||||||
|
||||||||||
Starting with Python 3.13.0b2, the offical macOS and Windows installers | ||||||||||
colesbury marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
optionally support installing free-threaded Python binaries. The installers | ||||||||||
are available at https://www.python.org/downloads/. | ||||||||||
|
||||||||||
.. seealso:: | ||||||||||
|
||||||||||
`Installing a Free-Threaded Python | ||||||||||
<https://py-free-threading.github.io/installing_cpython/>`_: | ||||||||||
A community-maintained installation guide for installing free-threaded | ||||||||||
Python. | ||||||||||
|
||||||||||
|
||||||||||
Identifying Free-Threaded Python | ||||||||||
colesbury marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
================================ | ||||||||||
|
||||||||||
The free-threaded build of CPython can optionally run with the global | ||||||||||
interpreter lock enabled, such as when :envvar:`PYTHON_GIL` is set to ``1``, | ||||||||||
or when importing an extension module that requires the GIL. | ||||||||||
|
||||||||||
|
||||||||||
The :func:`sys._is_gil_enabled` function will return ``False`` if the global | ||||||||||
interpreter lock is currently disabled. This is the recommended mechanism for | ||||||||||
decisions like whether to use multithreading or multiprocessing. | ||||||||||
|
||||||||||
|
||||||||||
The ``sysconfig.get_config_var("Py_GIL_DISABLED")`` configuration variable can | ||||||||||
be used to determine whether the build supports free threading. If the variable | ||||||||||
is set to ``1``, then the build supports free threading. This is the recommended | ||||||||||
mechanism for decisions related to the build configuration. | ||||||||||
|
||||||||||
|
||||||||||
Thread Safety | ||||||||||
|
||||||||||
============= | ||||||||||
|
||||||||||
The free-threaded build of CPython aims to provide similar thread-safety | ||||||||||
behavior at the Python level to the GIL-enabled build. Built-in | ||||||||||
colesbury marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
types like :class:`dict`, :class:`list`, and :class:`set` use internal locks | ||||||||||
to protect against concurrent modifications in ways that behave similarly to | ||||||||||
the GIL. However, Python has not historically guaranteed specific behavior for | ||||||||||
concurrent modifications to these built-in types, so this should be treated | ||||||||||
as a description of the current implementation, not a guarantee of future | ||||||||||
colesbury marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
behavior. | ||||||||||
|
||||||||||
.. note:: | ||||||||||
|
||||||||||
It's recommended to use the :class:`threading.Lock` or other synchronization | ||||||||||
willingc marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
primitives instead of relying on the internal locks of built-in types, when | ||||||||||
possible. | ||||||||||
|
||||||||||
|
||||||||||
|
||||||||||
Known Limitations | ||||||||||
colesbury marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
================= | ||||||||||
|
||||||||||
This section describes known limitations of the free-threaded CPython build. | ||||||||||
|
||||||||||
Immortalization | ||||||||||
--------------- | ||||||||||
|
||||||||||
The free-threaded build of the 3.13 release makes some objects :term:`immortal` | ||||||||||
in order to avoid reference count contention that would prevent efficient | ||||||||||
|
in order to avoid reference count contention that would prevent efficient | |
which have reference counts that are never modified. Since the immortal object is guaranteed not to be | |
deallocated, efficient multi-threading scaling is possible by avoiding reference count contention between | |
threads. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've reworked this a bit. Would you please take another look at it? As you wrote, immortalization means reference counts are not modified and objects are not guaranteed not to be deallocated. I wanted to get across that:
- Reference counts not being modified enables efficient multi-threaded scaling.
- Objects not being deallocated is a potential problem because some applications may leak memory. That's being addressed in 3.14. We don't expect this to be a problem for most applications, but it's something to watch out for if you run your application with refleak checks.
colesbury marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
ZeroIntensity marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
colesbury marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably worth noting somewhere in here that these objects (and immortalization itself) are implementation details, and very much subject to change.
ZeroIntensity marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After reading about immortalization, I don't understand the implications for me as a Python programmer. Why do I care that they are now immortal? These all sound like things that would have never been deallocated anyway. Are there unusual circumstances that I should be considering?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hopefully most people won't care, but some programs create these sorts of things in a loop.
colesbury marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
willingc marked this conversation as resolved.
Show resolved
Hide resolved
ZeroIntensity marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the possible crashing of the interpreter expected to be addressed in 3.14?
As a consequence of iterators not being thread safe, some modules are not safe either (e g. json
, itertools
). Should this be mentioned somewhere?
Uh oh!
There was an error while loading. Please reload this page.