Skip to content

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

Merged
merged 13 commits into from
Sep 25, 2024
Prev Previous commit
Next Next commit
Update docs
  • Loading branch information
colesbury committed Sep 24, 2024
commit 3b173e0d648cc447e82d6fc120f458552ffca62d
25 changes: 17 additions & 8 deletions Doc/howto/free-threading-python.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
.. _freethreading-python-howto:

*********************************
Python support for free threading
*********************************
**********************************************
Python experimental 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
with the :term:`global interpreter lock` (GIL) disabled in a build of Python
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seem to switch between "free threading" and "free-threaded" kind of randomly. I can't decide if this is OK or if we should choose one and stick with it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine. We also say both "multithreaded" and "multithreading" depending on the context.


Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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:

  • performance conscious users may wish to try the free threaded version
  • most users can safely continue to use the default CPython version
  • when free threading is no longer experimental, the versions will converge into one version for all.

.. seealso::

:pep:`703` – Making the Global Interpreter Lock Optional in CPython for an
overall description of free-threaded Python.


Installation
============
Expand All @@ -31,7 +36,7 @@ Identifying free-threaded Python

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.
or automatically 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
Expand Down Expand Up @@ -97,8 +102,11 @@ Frame objects
-------------

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

Iterators
---------
Expand All @@ -116,4 +124,5 @@ compared to the default GIL-enabled build. In 3.13, this overhead is about
40% on the `pyperformance <https://pyperformance.readthedocs.io/>`_ suite.
Programs that spend most of their time in C extensions or I/O will see
less of an impact. This overhead is expected to be reduced in Python
3.14.
3.14. We are aiming for an overhead of 10% or less on the pyperformance
suite compared to the default GIL-enabled build.
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy