SQLAlchemy threaded session execute does not commit changes

Viewed 186

I have the following simple setup

from concurrent.futures import ThreadPoolExecutor
from functools import partial

from sqlalchemy.orm import sessionmaker, scoped_session


def do_query():
    engine = ...

    session_factory = sessionmaker(bind=engine, autocommit=False, autoflush=False)
    ThreadedSession = scoped_session(session_factory)

    f = partial(
        _query_function,
        session=ThreadedSession,
    )

    queries = [...]

    with ThreadPoolExecutor(max_workers=num_threads) as pool:
        pool.map(f, queries)

    ThreadedSession.commit()


def _query_function(query, session):
    s = session()
    s.execute(query)
    return

Where I will pass the queries to a ThreadPoolExecutor, and have each thread use the shared session factory as in https://docs.sqlalchemy.org/en/13/orm/contextual.html#contextual-thread-local-sessions. However, the changes are not committed like this. Why?

0 Answers
Related