Replies: 1 comment 2 replies
-
Hey @paultiq, thanks for starting the discussion. We've just started experimenting with / learning about free-threaded python in the past days. Moving to free-threading seems likely to be a relatively large project and we haven't figured out how we want to approach it just yet, so don't expect changes very soon. Nonetheless this is very much on our radar and your input is much appreciated. I'd love to have a browse through your PR when you have it. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Per the contributing guidelines, I'd like to propose implementing free threading support for duckdb-python... at least, as an experimental feature.
Python 3.13 released experimental support for free-threading. In Python 3.14 free-threaded Python is officially supported
I have a working fork that's functional, satisfies a set of race tests, that I'm (nearly) ready to open a PR for.
* I also understand if this is something the team will defer, implement internally or otherwise. I'm just hoping to make this a reality sooner rather than later!
** 3.14 is in RC2 with a release scheduled for Oct 7, 2025. Some dependencies haven't yet shipped 3.14 support yet: to make this work, I had to mark pandas, polars, pyarrow and gcovr as
python_version < '3.14'
.Current Behavior
The GIL is automatically enabled when duckdb is imported in a free-threaded build of Python:
Summary
This change allows free-threaded applications to import and use DuckDB.
Free-threading support does not impact DuckDB itself in any significant way: DuckDB already releases the GIL during query execution, and DuckDB's concurrency wouldn't be changed.
Proposed Behavior / Changes
Module Declaration: Pass the required pybind11 flag to denote free threading support:
PYBIND11_MODULE(DUCKDB_PYTHON_LIB_NAME, m, py::mod_gil_not_used()) {
Module State Management: Move module state (helpfully marked
// NOLINT: allow global
in pyconnection.cpp) into a DuckDBPyModuleState object for multi-phase initCritical Sections: Add
py::scoped_critical_section
around specific shared operations, such as connection creation/cleanup, import cache, etc. **. So far, the changes have been fairly localized, owing a lot to duckdb's thread safety.Environmental: Some CI and configuration changes will be needed - especially in this early stage when not all dependencies have 3.14 support out yet.
Testing: An additional set of tests in
tests/fast/freethreading
CI: Add a 3.14t wheel, and set the
py::mod_gil_not_used()
tag only for 3.14+ to prevent inadvertently adding 3.13t support.Beta Was this translation helpful? Give feedback.
All reactions