An asyncio proxy for a :class:`_engine.Engine`. :class:`_asyncio.AsyncEngine` is acquired using the :func:`_asyncio.create_async_engine` function:: from sqlalchemy.ext.asyncio import create_async_engine engine = create_async_engine("postgresql+asyncpg://user:pass@host/dbna
| 997 | ) |
| 998 | # "Class has incompatible disjoint bases" - no idea |
| 999 | class AsyncEngine(ProxyComparable[Engine], AsyncConnectable): # type: ignore[misc] # noqa:E501 |
| 1000 | """An asyncio proxy for a :class:`_engine.Engine`. |
| 1001 | |
| 1002 | :class:`_asyncio.AsyncEngine` is acquired using the |
| 1003 | :func:`_asyncio.create_async_engine` function:: |
| 1004 | |
| 1005 | from sqlalchemy.ext.asyncio import create_async_engine |
| 1006 | |
| 1007 | engine = create_async_engine("postgresql+asyncpg://user:pass@host/dbname") |
| 1008 | |
| 1009 | .. versionadded:: 1.4 |
| 1010 | |
| 1011 | """ # noqa |
| 1012 | |
| 1013 | # AsyncEngine is a thin proxy; no state should be added here |
| 1014 | # that is not retrievable from the "sync" engine / connection, e.g. |
| 1015 | # current transaction, info, etc. It should be possible to |
| 1016 | # create a new AsyncEngine that matches this one given only the |
| 1017 | # "sync" elements. |
| 1018 | __slots__ = "sync_engine" |
| 1019 | |
| 1020 | _connection_cls: Type[AsyncConnection] = AsyncConnection |
| 1021 | |
| 1022 | sync_engine: Engine |
| 1023 | """Reference to the sync-style :class:`_engine.Engine` this |
| 1024 | :class:`_asyncio.AsyncEngine` proxies requests towards. |
| 1025 | |
| 1026 | This instance can be used as an event target. |
| 1027 | |
| 1028 | .. seealso:: |
| 1029 | |
| 1030 | :ref:`asyncio_events` |
| 1031 | """ |
| 1032 | |
| 1033 | def __init__(self, sync_engine: Engine): |
| 1034 | if not sync_engine.dialect.is_async: |
| 1035 | raise exc.InvalidRequestError( |
| 1036 | "The asyncio extension requires an async driver to be used. " |
| 1037 | f"The loaded {sync_engine.dialect.driver!r} is not async." |
| 1038 | ) |
| 1039 | self.sync_engine = self._assign_proxied(sync_engine) |
| 1040 | |
| 1041 | @util.ro_non_memoized_property |
| 1042 | def _proxied(self) -> Engine: |
| 1043 | return self.sync_engine |
| 1044 | |
| 1045 | @classmethod |
| 1046 | def _regenerate_proxy_for_target( |
| 1047 | cls, target: Engine, **additional_kw: Any # noqa: U100 |
| 1048 | ) -> AsyncEngine: |
| 1049 | return AsyncEngine(target) |
| 1050 | |
| 1051 | @contextlib.asynccontextmanager |
| 1052 | async def begin(self) -> AsyncIterator[AsyncConnection]: |
| 1053 | """Return a context manager which when entered will deliver an |
| 1054 | :class:`_asyncio.AsyncConnection` with an |
| 1055 | :class:`_asyncio.AsyncTransaction` established. |
| 1056 |
no outgoing calls