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