From 1aa67786fe42a2f7d29c61c7a91392de64f4a65f Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 9 Feb 2026 23:46:40 +0900 Subject: [PATCH] free threading: do not enable GIL --- README.md | 11 +++++++++++ src/MySQLdb/_mysql.c | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e679b533..9bc2fd43 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,17 @@ Or when you have question about MySQL/MariaDB: * [MySQL Support](https://dev.mysql.com/support/) * [Getting Help With MariaDB](https://mariadb.com/kb/en/getting-help-with-mariadb/) +## Free threading Python + +> [!NOTE] +> The support for Free threading Python is experimental. + +Since v2.2.8, free threading is supported when importing the extension module. Therefore, importing MySQLdb does not acquire GIL. + +However, this library does not support simultaneous operations on a single Connection object from multiple threads, regardless of free threading, and the behavior in such cases remains undefined. + +> [!WARNING] +> Do not use a single Connection object from multiple threads simultaneously. ## Install diff --git a/src/MySQLdb/_mysql.c b/src/MySQLdb/_mysql.c index cd95b641..30b111e5 100644 --- a/src/MySQLdb/_mysql.c +++ b/src/MySQLdb/_mysql.c @@ -2911,7 +2911,9 @@ PyInit__mysql(void) module = PyModule_Create(&_mysqlmodule); if (!module) return module; /* this really should never happen */ - +#ifdef Py_GIL_DISABLED + PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED); +#endif if (!(dict = PyModule_GetDict(module))) goto error; if (PyDict_SetItemString(dict, "version_info", PyRun_String(QUOTE(version_info), Py_eval_input,