|
| 1 | +.. _freethreading-python-howto: |
| 2 | + |
| 3 | +********************************************** |
| 4 | +Python experimental support for free threading |
| 5 | +********************************************** |
| 6 | + |
| 7 | +Starting with the 3.13 release, CPython has experimental 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 experimental** and work is ongoing to improve it: |
| 15 | +expect some bugs and a substantial single-threaded performance hit. |
| 16 | + |
| 17 | +This document describes the implications of free threading |
| 18 | +for Python code. See :ref:`freethreading-extensions-howto` for information on |
| 19 | +how to write C extensions that support the free-threaded build. |
| 20 | + |
| 21 | +.. seealso:: |
| 22 | + |
| 23 | + :pep:`703` – Making the Global Interpreter Lock Optional in CPython for an |
| 24 | + overall description of free-threaded Python. |
| 25 | + |
| 26 | + |
| 27 | +Installation |
| 28 | +============ |
| 29 | + |
| 30 | +Starting with Python 3.13, the official macOS and Windows installers |
| 31 | +optionally support installing free-threaded Python binaries. The installers |
| 32 | +are available at https://www.python.org/downloads/. |
| 33 | + |
| 34 | +For information on other platforms, see the `Installing a Free-Threaded Python |
| 35 | +<https://py-free-threading.github.io/installing_cpython/>`_, a |
| 36 | +community-maintained installation guide for installing free-threaded Python. |
| 37 | + |
| 38 | +When building CPython from source, the :option:`--disable-gil` configure option |
| 39 | +should be used to build a free-threaded Python interpreter. |
| 40 | + |
| 41 | + |
| 42 | +Identifying free-threaded Python |
| 43 | +================================ |
| 44 | + |
| 45 | +To check if the current interpreter supports free-threading, :option:`python -VV <-V>` |
| 46 | +and :attr:`sys.version` contain "experimental free-threading build". |
| 47 | +The new :func:`sys._is_gil_enabled` function can be used to check whether |
| 48 | +the GIL is actually disabled in the running process. |
| 49 | + |
| 50 | +The ``sysconfig.get_config_var("Py_GIL_DISABLED")`` configuration variable can |
| 51 | +be used to determine whether the build supports free threading. If the variable |
| 52 | +is set to ``1``, then the build supports free threading. This is the recommended |
| 53 | +mechanism for decisions related to the build configuration. |
| 54 | + |
| 55 | + |
| 56 | +The global interpreter lock in free-threaded Python |
| 57 | +=================================================== |
| 58 | + |
| 59 | +Free-threaded builds of CPython support optionally running with the GIL enabled |
| 60 | +at runtime using the environment variable :envvar:`PYTHON_GIL` or |
| 61 | +the command-line option :option:`-X gil`. |
| 62 | + |
| 63 | +The GIL may also automatically be enabled when importing a C-API extension |
| 64 | +module that is not explicitly marked as supporting free threading. A warning |
| 65 | +will be printed in this case. |
| 66 | + |
| 67 | +In addition to individual package documentation, the following websites track |
| 68 | +the status of popular packages support for free threading: |
| 69 | + |
| 70 | +* https://py-free-threading.github.io/tracking/ |
| 71 | +* https://hugovk.github.io/free-threaded-wheels/ |
| 72 | + |
| 73 | + |
| 74 | +Thread safety |
| 75 | +============= |
| 76 | + |
| 77 | +The free-threaded build of CPython aims to provide similar thread-safety |
| 78 | +behavior at the Python level to the default GIL-enabled build. Built-in |
| 79 | +types like :class:`dict`, :class:`list`, and :class:`set` use internal locks |
| 80 | +to protect against concurrent modifications in ways that behave similarly to |
| 81 | +the GIL. However, Python has not historically guaranteed specific behavior for |
| 82 | +concurrent modifications to these built-in types, so this should be treated |
| 83 | +as a description of the current implementation, not a guarantee of current or |
| 84 | +future behavior. |
| 85 | + |
| 86 | +.. note:: |
| 87 | + |
| 88 | + It's recommended to use the :class:`threading.Lock` or other synchronization |
| 89 | + primitives instead of relying on the internal locks of built-in types, when |
| 90 | + possible. |
| 91 | + |
| 92 | + |
| 93 | +Known limitations |
| 94 | +================= |
| 95 | + |
| 96 | +This section describes known limitations of the free-threaded CPython build. |
| 97 | + |
| 98 | +Immortalization |
| 99 | +--------------- |
| 100 | + |
| 101 | +The free-threaded build of the 3.13 release makes some objects :term:`immortal`. |
| 102 | +Immortal objects are not deallocated and have reference counts that are |
| 103 | +never modified. This is done to avoid reference count contention that would |
| 104 | +prevent efficient multi-threaded scaling. |
| 105 | + |
| 106 | +An object will be made immortal when a new thread is started for the first time |
| 107 | +after the main thread is running. The following objects are immortalized: |
| 108 | + |
| 109 | +* :ref:`function <user-defined-funcs>` objects declared at the module level |
| 110 | +* :ref:`method <instance-methods>` descriptors |
| 111 | +* :ref:`code <code-objects>` objects |
| 112 | +* :term:`module` objects and their dictionaries |
| 113 | +* :ref:`classes <classes>` (type objects) |
| 114 | + |
| 115 | +Because immortal objects are never deallocated, applications that create many |
| 116 | +objects of these types may see increased memory usage. This is expected to be |
| 117 | +addressed in the 3.14 release. |
| 118 | + |
| 119 | +Additionally, numeric and string literals in the code as well as strings |
| 120 | +returned by :func:`sys.intern` are also immortalized. This behavior is |
| 121 | +expected to remain in the 3.14 free-threaded build. |
| 122 | + |
| 123 | + |
| 124 | +Frame objects |
| 125 | +------------- |
| 126 | + |
| 127 | +It is not safe to access :ref:`frame <frame-objects>` objects from other |
| 128 | +threads and doing so may cause your program to crash . This means that |
| 129 | +:func:`sys._current_frames` is generally not safe to use in a free-threaded |
| 130 | +build. Functions like :func:`inspect.currentframe` and :func:`sys._getframe` |
| 131 | +are generally safe as long as the resulting frame object is not passed to |
| 132 | +another thread. |
| 133 | + |
| 134 | +Iterators |
| 135 | +--------- |
| 136 | + |
| 137 | +Sharing the same iterator object between multiple threads is generally not |
| 138 | +safe and threads may see duplicate or missing elements when iterating or crash |
| 139 | +the interpreter. |
| 140 | + |
| 141 | + |
| 142 | +Single-threaded performance |
| 143 | +--------------------------- |
| 144 | + |
| 145 | +The free-threaded build has additional overhead when executing Python code |
| 146 | +compared to the default GIL-enabled build. In 3.13, this overhead is about |
| 147 | +40% on the `pyperformance <https://pyperformance.readthedocs.io/>`_ suite. |
| 148 | +Programs that spend most of their time in C extensions or I/O will see |
| 149 | +less of an impact. The largest impact is because the specializing adaptive |
| 150 | +interpreter (:pep:`659`) is disabled in the free-threaded build. We expect |
| 151 | +to re-enable it in a thread-safe way in the 3.14 release. This overhead is |
| 152 | +expected to be reduced in upcoming Python release. We are aiming for an |
| 153 | +overhead of 10% or less on the pyperformance suite compared to the default |
| 154 | +GIL-enabled build. |
0 commit comments