Skip to content

Instantly share code, notes, and snippets.

@artofhuman
Created April 12, 2023 09:11
Show Gist options
  • Save artofhuman/a43b8abaac05ade524680a8b4a10566f to your computer and use it in GitHub Desktop.
Save artofhuman/a43b8abaac05ade524680a8b4a10566f to your computer and use it in GitHub Desktop.
import os
def create_db_engine(db_url, **kwargs):
return create_engine(
url=db_url, future=True, **kwargs
)
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_db_engine(os.environ.get("TEST_DATABASE_URL"), isolation_level="AUTOCOMMIT")
SessionLocal = sessionmaker(
autocommit=False, autoflush=False, bind=engine, future=True, expire_on_commit=False
)
import concurrent.futures
from app.db import transaction
def worker():
db = SessionLocal()
with transaction(db):
db.execute("select 1")
print(db.connection().connection.connection)
with concurrent.futures.ThreadPoolExecutor() as exec:
f1 = exec.submit(worker)
f2 = exec.submit(worker)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment