|
| 1 | +--- |
| 2 | +title: 'The first year of free-threaded Python' |
| 3 | +authors: [nathan-goldbaum] |
| 4 | +published: May 13, 2025 |
| 5 | +description: 'A recap of the first year of work on enabling support for the free-threaded build of CPython in community packages.' |
| 6 | +category: [Community, PyData ecosystem] |
| 7 | +featuredImage: |
| 8 | + src: /posts/free-threaded-one-year-recap/snake-cartoon.jpg |
| 9 | + alt: 'A cartoon of a Python wrapped around a thread, illustrating Python and threads getting along.' |
| 10 | +hero: |
| 11 | + imageSrc: /posts/free-threaded-one-year-recap/snake-cartoon.jpg |
| 12 | + imageAlt: 'A cartoon of a Python wrapped around a thread, illustrating Python and threads getting along.' |
| 13 | +--- |
| 14 | + |
| 15 | +Last week, the CPython developers rolled out CPython 3.14.0b1. This week, PyCon |
| 16 | +2025 kicks off in Pittsburgh, PA. Both events mark a significant milestone for |
| 17 | +the effort to ship and stabilize free-threaded Python. |
| 18 | + |
| 19 | +This is the story of the first year of that effort and how our team at |
| 20 | +Quansight played a key role in enabling experimental use of the free-threaded |
| 21 | +build with real production workflows that depend on a complex set of |
| 22 | +dependencies. |
| 23 | + |
| 24 | +## Introduction: Why are we working on community support for free-threaded Python? |
| 25 | + |
| 26 | +Support for free-threaded Python unlocks the full compute power of modern |
| 27 | +hardware with multicore CPUs and GPUs now commonplace. In the GIL-enabled |
| 28 | +build, making full use of parallel algorithms that exploit all available |
| 29 | +compute resources in Python requires workarounds and careful tuning. The Python |
| 30 | +`threading` module is often not used because the GIL prevents useful parallel |
| 31 | +scaling. Instead, many reach for `multiprocessing`, but spawning processes is |
| 32 | +expensive and communicating across processes often requires making expensive |
| 33 | +copies of data that would not be necessary in a multithreaded program where |
| 34 | +data is implicitly shared between threads. |
| 35 | + |
| 36 | +Having said that, it is not possible for packages that ship compiled code in |
| 37 | +their release distributions to support the free-threaded build out of the box. |
| 38 | +Any package shipping native code (many Python packages do that) will need to be |
| 39 | +audited to ensure the package builds and either fix or document the safety |
| 40 | +guarantees provided by the package. Disabling the GIL required deep structural |
| 41 | +changes to the CPython interpreter. Fully supporting the free-threaded build in |
| 42 | +existing packages similarly requires fixing structural issues that until now |
| 43 | +were not big problems. Things like use of global state in the implementation of |
| 44 | +a C extension for convenience or for performance are no longer safe, since the |
| 45 | +GIL does not protect simultaneous access from Python to the global state, |
| 46 | +allowing undefined behavior via data races. While it is possible to trigger |
| 47 | +thread safety issues like this using the `threading` module even with the GIL, |
| 48 | +most of the time the GIL prevented these issues from surfacing. The |
| 49 | +free-threaded build makes fixing these issues much more pressing. |
| 50 | + |
| 51 | +## Major accomplishments |
| 52 | + |
| 53 | +Alongside the Python runtime team at Meta, we made significant contributions to |
| 54 | +enable support for free-threaded Python in a long list of packages and |
| 55 | +projects, including: |
| 56 | + |
| 57 | + * Packaging and project workflow tools like meson, meson-python, the |
| 58 | + setup-python GitHub workflow, packaging, pip, and setuptools. |
| 59 | + * Bindings generators like Cython, pybind11, f2py, and PyO3. |
| 60 | + * Foundational packages in the PyData ecosystem like NumPy, SciPy, PyArrow, |
| 61 | + Matplotlib, pandas, scikit-learn, and scikit-image. |
| 62 | + * Top dependencies by PyPI downloads like Pillow, PyYAML, yarl, multidict, |
| 63 | + and frozenlist. |
| 64 | + |
| 65 | +We are also currently looking at popular packages that don't yet ship support, |
| 66 | +including CFFI, cryptography, PyNaCl, aiohttp, SQLAlchemy, and grpcio as well |
| 67 | +as popular libraries for machine learning workflows like safetensors and |
| 68 | +tokenizers. |
| 69 | + |
| 70 | +CPython core developers on our team also contributed several major improvements |
| 71 | +that will ship in CPython 3.14: |
| 72 | + |
| 73 | + * The Python `warnings` module is now thread-safe by default on the |
| 74 | + free-threaded build. It can be made thread-safe on the GIL-enabled build |
| 75 | + with a configuration option or runtime command-line flag. |
| 76 | + * Significant thread safety issues in `asyncio` have been fixed. Our |
| 77 | + benchmarks indicate substantially improved parallel scaling of code using |
| 78 | + asyncio with a thread pool runner as a function of thread count. |
| 79 | + * Thread safety overhaul in the `ctypes` module. |
| 80 | + * Substantial performance improvements for the free-threaded garbage collector. |
| 81 | + * Helped implement the deferred reference counting scheme used by the |
| 82 | + free-threaded interpreter in 3.14. |
| 83 | + * Implemented several specializations for the adaptive specializing |
| 84 | + interpreter and supported shipping optimizations that bring the |
| 85 | + single-threaded performance of free-threaded CPython 3.14 within spitting |
| 86 | + distance of the GIL-enabled build. |
| 87 | + * A huge number of smaller bugfixes and thread safety improvements. |
| 88 | + |
| 89 | +We've also written a [comprehensive guide](https://py-free-threading.github.io) |
| 90 | +for supporting free-threading in existing apps and packages gleaned from our |
| 91 | +experiences. Our hope is that the documentation we've written can be a valuable |
| 92 | +resource for the "long tail" of packages that people will want to update to |
| 93 | +support free-threaded Python in the coming years. |
| 94 | + |
| 95 | +You can also read more about this effort from the team at Meta on the |
| 96 | +[Meta engineering blog](https://engineering.fb.com/2025/05/05/developer-tools/enhancing-the-python-ecosystem-with-type-checking-and-free-threading/). |
| 97 | + |
| 98 | +## What is the state of the free-threaded Python ecosystem? |
| 99 | + |
| 100 | +At this time last year, when Python 3.13.0b1 shipped, the wider ecosystem of |
| 101 | +Python packages was more or less completely broken on the free-threaded build. |
| 102 | +Trying to `pip install` anything but the simplest package with no dependencies |
| 103 | +or only pure-Python dependencies would likely lead to build errors. Most of |
| 104 | +these issues were not due to fundamental problems but because of unsupported |
| 105 | +default options or minor assumptions broken on the free-threaded build. |
| 106 | + |
| 107 | +Together with package maintainers and other contributors in the community, |
| 108 | +we have fixed all of these issues and today things are much better. With the |
| 109 | +release of Cython 3.1.0, which ships official support for the free-threaded |
| 110 | +build, we also helped fix one of the most significant source of build issues. |
| 111 | + |
| 112 | +We are currently working on packages that ship compiled code but still do not |
| 113 | +yet ship free-threaded wheels. You can track our progress using our manually |
| 114 | +updated [status tracking table](https://py-free-threading.github.io/tracking/) |
| 115 | +or using Hugo van Kemenade's [automatically updated |
| 116 | +tracker](https://hugovk.github.io/free-threaded-wheels/). |
| 117 | + |
| 118 | +### Challenges |
| 119 | + |
| 120 | +As of today, the free-threaded Python build is ready to experiment with. We |
| 121 | +need more reports of bad performance and bugs from people with real-world |
| 122 | +workflows. Significant performance improvements are possible, particularly in |
| 123 | +workflows that make use of multiprocessing and are paying the costs inherent to |
| 124 | +that approach. However, many packages still need detailed auditing to discover |
| 125 | +thread safety issues. Many Python libraries ship mutable data structures that |
| 126 | +will not behave correctly under shared mutating and with no or minimal |
| 127 | +documentation on thread safety or multithreaded performance. |
| 128 | + |
| 129 | +As in any change of this magnitude that affects an entire programming language |
| 130 | +package ecosystem, we are hitting cases where popular packages do not have the |
| 131 | +resources needed to deal with changes needed to support free-threading. This is |
| 132 | +particularly true of large legacy packages where few people or even no one |
| 133 | +fully understands the code. As a community, we need to understand these issues |
| 134 | +in our dependency trees and work towards sustainable maintenance for critical |
| 135 | +packages. |
| 136 | + |
| 137 | +### How can you help? |
| 138 | + |
| 139 | +Take a look at the [contribution guide](https://py-free-threading.github.io/contributing/) |
| 140 | +we've added to the main free-threading guide. We're tracking ecosystem-wide |
| 141 | +issues and writing the content of the free-threaded guide in the |
| 142 | +[free-threaded-compatibility](https://github.com/Quansight-Labs/free-threaded-compatibility) |
| 143 | +repository hosted on the Quansight-Labs GitHub org. |
| 144 | + |
| 145 | +We also helped launch a [community Discord](https://discord.gg/rqgHCDqdRr) to host |
| 146 | +discussions about supporting the free-threaded build. Come join us if you're |
| 147 | +interested in helping out! |
| 148 | + |
| 149 | +## Come to our talk at PyCon! |
| 150 | + |
| 151 | +I will be giving [a talk at |
| 152 | +PyCon](https://us.pycon.org/2025/schedule/presentation/42/) with my teammate |
| 153 | +[Lysandros Nikolaou](https://github.com/lysnikolaou). If you'll be attending |
| 154 | +the conference, please come and watch. We'll be sharing details from our |
| 155 | +experiences porting packages to support the free-threaded build. We're hopeful |
| 156 | +that the recording on YouTube will be a lasting valuable resource for the |
| 157 | +visual learners of the world. |
| 158 | + |
| 159 | +Personally, I believe the free-threaded build is the future of the language, |
| 160 | +and am excited that I get to work full-time on enabling that. I'm also hopeful |
| 161 | +that the work we're doing now will enable future work in the long tail of |
| 162 | +packages used every day by millions of developers and dramatically improve the |
| 163 | +performance of the language. |
0 commit comments