Chapter 7 of the SQLAlchemy 2 in Practice book covers how to adapt SQLAlchemy applications to use Python's asyncio model. It explains the concept of function coloring and why all layers of an async app must be non-blocking. Topics include: choosing async database drivers (aiosqlite, aiomysql, asyncpg), replacing synchronous engine/session with create_async_engine and async_sessionmaker, setting expire_on_commit=False to avoid implicit lazy loads, reconfiguring ORM relationship loaders (replacing lazy='select' with joined, selectin, or write_only), configuring Alembic with its async template, handling the post-flush implicit I/O problem by pre-initializing list relationships, and using stream()/stream_scalars() for efficient async result iteration.
Table of contents
How is Async Different?Asynchronous Database DriversEngines, Metadata and SessionsRelationships LoadersAlembic ConfigurationImplicit I/O After a Session is FlushedImport ScriptsQueriesSort: