create_async_session#

async safir.database.create_async_session(engine, logger=None, *, statement=None)#

Create a new async database session.

Optionally checks that the database is available and retries in a loop for 10s if it is not. This should be used during application startup to wait for any network setup or database proxy sidecar.

Parameters:
  • engine (AsyncEngine) – Database engine to use for the session.

  • logger (Optional[BoundLogger], default: None) – Logger for reporting errors. Used only if a statement is provided.

  • statement (Optional[Select], default: None) – If provided, statement to run to check database connectivity. This will be modified with limit(1) before execution. If not provided, database connectivity will not be checked.

Returns:

A newly-created async session. It can be explicitly disposed with await session.close() when the caller is done with the session.

Return type:

sqlalchemy.ext.asyncio.AsyncSession