|
| 1 | +.. _freethreading-python-howto: |
| 2 | + |
| 3 | +********************************* |
| 4 | +Python support for free threading |
| 5 | +********************************* |
| 6 | + |
| 7 | +Starting with the 3.13 release, CPython has support for a build of |
| 8 | +Python called :term:`free threading` where the :term:`global interpreter lock` |
| 9 | +(GIL) is disabled. Free-threaded execution allows for full utilization of the |
| 10 | +available processing power by running threads in parallel on available CPU cores. |
| 11 | +While not all software will benefit from this automatically, programs |
| 12 | +designed with threading in mind will run faster on multi-core hardware. |
| 13 | + |
| 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 |
| 17 | +with an :term:`extension module`, may not be ready for use in a |
| 18 | +free-threaded build, and will re-enable the :term:`GIL`. |
| 19 | + |
| 20 | +This document describes the implications of free threading |
| 21 | +for Python code. See :ref:`freethreading-extensions-howto` for information on |
| 22 | +how to write C extensions that support the free-threaded build. |
| 23 | + |
| 24 | +.. seealso:: |
| 25 | + |
| 26 | + :pep:`703` – Making the Global Interpreter Lock Optional in CPython for an |
| 27 | + overall description of free-threaded Python. |
| 28 | + |
| 29 | + |
| 30 | +Installation |
| 31 | +============ |
| 32 | + |
| 33 | +Starting with Python 3.13, the official macOS and Windows installers |
| 34 | +optionally support installing free-threaded Python binaries. The installers |
| 35 | +are available at https://www.python.org/downloads/. |
| 36 | + |
| 37 | +For information on other platforms, see the `Installing a Free-Threaded Python |
| 38 | +<https://py-free-threading.github.io/installing-cpython/>`_, a |
| 39 | +community-maintained installation guide for installing free-threaded Python. |
| 40 | + |
| 41 | +When building CPython from source, the :option:`--disable-gil` configure option |
| 42 | +should be used to build a free-threaded Python interpreter. |
| 43 | + |
| 44 | + |
| 45 | +Identifying free-threaded Python |
| 46 | +================================ |
| 47 | + |
| 48 | +To check if the current interpreter supports free-threading, :option:`python -VV <-V>` |
| 49 | +and :data:`sys.version` contain "free-threading build". |
| 50 | +The new :func:`sys._is_gil_enabled` function can be used to check whether |
| 51 | +the GIL is actually disabled in the running process. |
| 52 | + |
| 53 | +The ``sysconfig.get_config_var("Py_GIL_DISABLED")`` configuration variable can |
| 54 | +be used to determine whether the build supports free threading. If the variable |
| 55 | +is set to ``1``, then the build supports free threading. This is the recommended |
| 56 | +mechanism for decisions related to the build configuration. |
| 57 | + |
| 58 | + |
| 59 | +The global interpreter lock in free-threaded Python |
| 60 | +=================================================== |
| 61 | + |
| 62 | +Free-threaded builds of CPython support optionally running with the GIL enabled |
| 63 | +at runtime using the environment variable :envvar:`PYTHON_GIL` or |
| 64 | +the command-line option :option:`-X gil`. |
| 65 | + |
| 66 | +The GIL may also automatically be enabled when importing a C-API extension |
| 67 | +module that is not explicitly marked as supporting free threading. A warning |
| 68 | +will be printed in this case. |
| 69 | + |
| 70 | +In addition to individual package documentation, the following websites track |
| 71 | +the status of popular packages support for free threading: |
| 72 | + |
| 73 | +* https://py-free-threading.github.io/tracking/ |
| 74 | +* https://hugovk.github.io/free-threaded-wheels/ |
| 75 | + |
| 76 | + |
| 77 | +Thread safety |
| 78 | +============= |
| 79 | + |
| 80 | +The free-threaded build of CPython aims to provide similar thread-safety |
| 81 | +behavior at the Python level to the default GIL-enabled build. Built-in |
| 82 | +types like :class:`dict`, :class:`list`, and :class:`set` use internal locks |
| 83 | +to protect against concurrent modifications in ways that behave similarly to |
| 84 | +the GIL. However, Python has not historically guaranteed specific behavior for |
| 85 | +concurrent modifications to these built-in types, so this should be treated |
| 86 | +as a description of the current implementation, not a guarantee of current or |
| 87 | +future behavior. |
| 88 | + |
| 89 | +.. note:: |
| 90 | + |
| 91 | + It's recommended to use the :class:`threading.Lock` or other synchronization |
| 92 | + primitives instead of relying on the internal locks of built-in types, when |
| 93 | + possible. |
| 94 | + |
| 95 | + |
| 96 | +Known limitations |
| 97 | +================= |
| 98 | + |
| 99 | +This section describes known limitations of the free-threaded CPython build. |
| 100 | + |
| 101 | +Immortalization |
| 102 | +--------------- |
| 103 | + |
| 104 | +The free-threaded build of the 3.13 release makes some objects :term:`immortal`. |
| 105 | +Immortal objects are not deallocated and have reference counts that are |
| 106 | +never modified. This is done to avoid reference count contention that would |
| 107 | +prevent efficient multi-threaded scaling. |
| 108 | + |
| 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: |
| 111 | + |
| 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. |
| 125 | + |
| 126 | + |
| 127 | +Frame objects |
| 128 | +------------- |
| 129 | + |
| 130 | +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 |
| 132 | +: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` |
| 134 | +are generally safe as long as the resulting frame object is not passed to |
| 135 | +another thread. |
| 136 | + |
| 137 | +Iterators |
| 138 | +--------- |
| 139 | + |
| 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. |
| 143 | + |
| 144 | + |
| 145 | +Single-threaded performance |
| 146 | +--------------------------- |
| 147 | + |
| 148 | +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. |
| 151 | +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. |
| 158 | + |
| 159 | + |
| 160 | +Behavioral changes |
| 161 | +================== |
| 162 | + |
| 163 | +This section describes CPython behavioural changes with the free-threaded |
| 164 | +build. |
| 165 | + |
| 166 | + |
| 167 | +Context variables |
| 168 | +----------------- |
| 169 | + |
| 170 | +In the free-threaded build, the flag :data:`~sys.flags.thread_inherit_context` |
| 171 | +is set to true by default which causes threads created with |
| 172 | +:class:`threading.Thread` to start with a copy of the |
| 173 | +:class:`~contextvars.Context()` of the caller of |
| 174 | +:meth:`~threading.Thread.start`. In the default GIL-enabled build, the flag |
| 175 | +defaults to false so threads start with an |
| 176 | +empty :class:`~contextvars.Context()`. |
| 177 | + |
| 178 | + |
| 179 | +Warning filters |
| 180 | +--------------- |
| 181 | + |
| 182 | +In the free-threaded build, the flag :data:`~sys.flags.context_aware_warnings` |
| 183 | +is set to true by default. In the default GIL-enabled build, the flag defaults |
| 184 | +to false. If the flag is true then the :class:`warnings.catch_warnings` |
| 185 | +context manager uses a context variable for warning filters. If the flag is |
| 186 | +false then :class:`~warnings.catch_warnings` modifies the global filters list, |
| 187 | +which is not thread-safe. See the :mod:`warnings` module for more details. |
0 commit comments