A configurable :class:`.AsyncSession` factory. The :class:`.async_sessionmaker` factory works in the same way as the :class:`.sessionmaker` factory, to generate new :class:`.AsyncSession` objects when called, creating them given the configurational arguments established here. e.g.:: from sqlalchemy.ext.asyncio import create_async_engine from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import async_sessionmaker async def run_some_sql(async_session: async_sessionmaker[AsyncSession]) -> None: async with async_session() as session: session.add(SomeObject(data="object")) session.add(SomeOtherObject(name="other object")) await session.commit() async def main() -> None: # an AsyncEngine, which the AsyncSession will use for connection # resources engine = create_async_engine('postgresql+asyncpg://scott:tiger@localhost/') # create a reusable factory for new AsyncSession instances async_session = async_sessionmaker(engine) await run_some_sql(async_session) await engine.dispose() The :class:`.async_sessionmaker` is useful so that different parts of a program can create new :class:`.AsyncSession` objects with a fixed configuration established up front. Note that :class:`.AsyncSession` objects may also be instantiated directly when not using :class:`.async_sessionmaker`. .. versionadded:: 2.0 :class:`.async_sessionmaker` provides a :class:`.sessionmaker` class that's dedicated to the :class:`.AsyncSession` object, including pep-484 typing support. .. seealso:: :ref:`asyncio_orm` - shows example use :class:`.sessionmaker` - general overview of the :class:`.sessionmaker` architecture :ref:`session_getting` - introductory text on creating sessions using :class:`.sessionmaker`.
Method | __call__ |
Produce a new :class:`.AsyncSession` object using the configuration established in this :class:`.async_sessionmaker`. |
Method | __init__ |
Construct a new :class:`.async_sessionmaker`. |
Method | __repr__ |
Undocumented |
Method | begin |
Produce a context manager that both provides a new :class:`_orm.AsyncSession` as well as a transaction that commits. |
Method | configure |
(Re)configure the arguments for this async_sessionmaker. |
Instance Variable | class_ |
Undocumented |
Instance Variable | kw |
Undocumented |
Produce a new :class:`.AsyncSession` object using the configuration established in this :class:`.async_sessionmaker`. In Python, the ``__call__`` method is invoked on an object when it is "called" in the same way as a function:: AsyncSession = async_sessionmaker(async_engine, expire_on_commit=False) session = AsyncSession() # invokes sessionmaker.__call__()
Optional[ _AsyncSessionBind]
= ..., *, class_: Type[ _AS]
, autoflush: bool
= ..., expire_on_commit: bool
= ..., info: Optional[ _InfoType]
= ..., **kw: Any
):async_sessionmaker[ AsyncSession]
, bind: Optional[ _AsyncSessionBind]
= ..., *, autoflush: bool
= ..., expire_on_commit: bool
= ..., info: Optional[ _InfoType]
= ..., **kw: Any
):Construct a new :class:`.async_sessionmaker`. All arguments here except for ``class_`` correspond to arguments accepted by :class:`.Session` directly. See the :meth:`.AsyncSession.__init__` docstring for more details on parameters.
Produce a context manager that both provides a new :class:`_orm.AsyncSession` as well as a transaction that commits. e.g.:: async def main(): Session = async_sessionmaker(some_engine) async with Session.begin() as session: session.add(some_object) # commits transaction, closes session