File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11from __future__ import absolute_import
22
3+ from asyncio import get_event_loop
4+ from concurrent .futures import ThreadPoolExecutor
35from typing import Any , Dict , Callable , Optional
46
57from sqlalchemy import create_engine
1416# therefore the "..." convention is used.
1517SessionFactoryType = Callable [..., SessionCommitter ]
1618
19+ SQLITE_DRIVER_NAME = 'pysqlite'
20+
1721
1822def create_session_factory_from_engine (engine : Engine ) -> SessionFactoryType :
1923 """
@@ -24,6 +28,13 @@ def create_session_factory_from_engine(engine: Engine) -> SessionFactoryType:
2428 """
2529 session_maker = sessionmaker (bind = engine )
2630
31+ # SQLite workaround: SQLite objects cannot be shared between threads
32+ if engine .driver == SQLITE_DRIVER_NAME :
33+ print ('WARNING: Sqlite backend detected. Using single threaded executor '
34+ '- DO NOT USE IN PRODUCTION!' )
35+ executor = ThreadPoolExecutor (max_workers = 1 )
36+ get_event_loop ().set_default_executor (executor )
37+
2738 def factory (reuse_session : Optional [Session ] = None ) -> SessionCommitter :
2839 """
2940 Create a session.
You can’t perform that action at this time.
0 commit comments