This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: [sqlite3] set threadsafety attribute based on default SQLite threaded mode
Type: Stage: resolved
Components: Extension Modules Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: erlendaasland Nosy List: erlendaasland, lemburg, pablogsal, paul.moore, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2021-10-26 12:04 by erlendaasland, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 29227 merged erlendaasland, 2021-10-26 12:36
Messages (5)
msg405035 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-10-26 12:04
Currently, the sqlite3 DB-API 2.0 attribute 'threadsafety' is hard-coded to 1, meaning "threads may share the module, but not connections". This is not always true, since it depends on the default SQLite threaded mode, selected at compile-time with the SQLITE_THREADSAFE define.

SQLite's default compile-time threaded mode is SQLITE_THREADSAFE=1, also known as "serialized threaded mode", meaning SQLite can be safely used by multiple threads with no restriction. This mode equals DB-API 2.0 threadsafety level 3: threads may share the module, connections and cursors.

On macOS 11.6, the system supplied Python 3 and SQLite 3 uses SQLITE_THREADSAFE=2 (also known as "multi-thread mode"), meaning SQLite can be safely used by multiple threads provided that no single database connection is used simultaneously in two or more threads. This mode equals DB-API 2.0 threadsafety level 1: threads may share the module, but not connections.

With SQLITE_THREADSAFE=0 (also known as "single-thread mode"), meaning all mutexes are disabled and SQLite is unsafe to use in more than a single thread at once. This mode equals DB-API 2.0 threadsafety level 0: threads may not share the module.


Suggesting to set the 'treadsafety' dynamically at sqlite3 module load time, either via Lib/sqlite3/dbapi2.py, or in C during module init (slightly faster).


See also:

- https://www.python.org/dev/peps/pep-0249/
- https://sqlite.org/threadsafe.html
- https://discuss.python.org/t/is-sqlite3-threadsafety-the-same-thing-as-sqlite3-threadsafe-from-the-c-library/11463/11
msg405039 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2021-10-26 12:09
+1 on setting the attributes dynamically. Other DB-API modules use a
similar approach.
msg405040 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-10-26 12:13
FYI, it is also possible to change the threaded mode using sqlite3_config(), however that API is not exposed to the sqlite3 module, and there is no plans for doing so in the near future :)
msg405041 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-10-26 12:41
BTW, I've verified that the overhead of adding this query is negligible.

$ python -m pyperf compare_to --min-speed 1 main.json patched.json 
Benchmark hidden because not significant (1): sqlite_synth

git switch -
Switched to branch 'main'
$ ./python.exe -m timeit "import sqlite3; sqlite3.threadsafety"
1000000 loops, best of 5: 225 nsec per loop
$ git switch -
Switched to branch 'sqlite-threadsafety'
$ ./python.exe -m timeit "import sqlite3; sqlite3.threadsafety"
1000000 loops, best of 5: 221 nsec per loop
msg405649 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-11-03 21:01
New changeset c2739867113a2b650db300c03ef06cf18dcee3f4 by Erlend Egeberg Aasland in branch 'main':
bpo-45613: Set `sqlite3.threadsafety` dynamically (GH-29227)
https://github.com/python/cpython/commit/c2739867113a2b650db300c03ef06cf18dcee3f4
History
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89776
2021-11-03 21:07:02erlendaaslandsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-11-03 21:01:46pablogsalsetnosy: + pablogsal
messages: + msg405649
2021-10-26 12:41:04erlendaaslandsetmessages: + msg405041
2021-10-26 12:36:23erlendaaslandsetkeywords: + patch
stage: patch review
pull_requests: + pull_request27491
2021-10-26 12:13:44erlendaaslandsetmessages: + msg405040
2021-10-26 12:09:26lemburgsetmessages: + msg405039
2021-10-26 12:04:50erlendaaslandsetnosy: + paul.moore
2021-10-26 12:04:37erlendaaslandsetnosy: + serhiy.storchaka
2021-10-26 12:04:26erlendaaslandcreate