Created
April 12, 2023 09:11
-
-
Save artofhuman/a43b8abaac05ade524680a8b4a10566f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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