asyncio.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. # connectors/asyncio.py
  2. # Copyright (C) 2005-2026 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. """generic asyncio-adapted versions of DBAPI connection and cursor"""
  8. from __future__ import annotations
  9. import asyncio
  10. import collections
  11. import sys
  12. from typing import Any
  13. from typing import AsyncIterator
  14. from typing import Deque
  15. from typing import Iterator
  16. from typing import NoReturn
  17. from typing import Optional
  18. from typing import Sequence
  19. from typing import Tuple
  20. from typing import Type
  21. from typing import TYPE_CHECKING
  22. from ..engine import AdaptedConnection
  23. from ..util import EMPTY_DICT
  24. from ..util.concurrency import await_fallback
  25. from ..util.concurrency import await_only
  26. from ..util.concurrency import in_greenlet
  27. from ..util.typing import Protocol
  28. if TYPE_CHECKING:
  29. from ..engine.interfaces import _DBAPICursorDescription
  30. from ..engine.interfaces import _DBAPIMultiExecuteParams
  31. from ..engine.interfaces import _DBAPISingleExecuteParams
  32. from ..engine.interfaces import DBAPIModule
  33. from ..util.typing import Self
  34. class AsyncIODBAPIConnection(Protocol):
  35. """protocol representing an async adapted version of a
  36. :pep:`249` database connection.
  37. """
  38. # note that async DBAPIs dont agree if close() should be awaitable,
  39. # so it is omitted here and picked up by the __getattr__ hook below
  40. async def commit(self) -> None: ...
  41. def cursor(self, *args: Any, **kwargs: Any) -> AsyncIODBAPICursor: ...
  42. async def rollback(self) -> None: ...
  43. def __getattr__(self, key: str) -> Any: ...
  44. def __setattr__(self, key: str, value: Any) -> None: ...
  45. class AsyncIODBAPICursor(Protocol):
  46. """protocol representing an async adapted version
  47. of a :pep:`249` database cursor.
  48. """
  49. def __aenter__(self) -> Any: ...
  50. @property
  51. def description(
  52. self,
  53. ) -> _DBAPICursorDescription:
  54. """The description attribute of the Cursor."""
  55. ...
  56. @property
  57. def rowcount(self) -> int: ...
  58. arraysize: int
  59. lastrowid: int
  60. async def close(self) -> None: ...
  61. async def execute(
  62. self,
  63. operation: Any,
  64. parameters: Optional[_DBAPISingleExecuteParams] = None,
  65. ) -> Any: ...
  66. async def executemany(
  67. self,
  68. operation: Any,
  69. parameters: _DBAPIMultiExecuteParams,
  70. ) -> Any: ...
  71. async def fetchone(self) -> Optional[Any]: ...
  72. async def fetchmany(self, size: Optional[int] = ...) -> Sequence[Any]: ...
  73. async def fetchall(self) -> Sequence[Any]: ...
  74. async def setinputsizes(self, sizes: Sequence[Any]) -> None: ...
  75. def setoutputsize(self, size: Any, column: Any) -> None: ...
  76. async def callproc(
  77. self, procname: str, parameters: Sequence[Any] = ...
  78. ) -> Any: ...
  79. async def nextset(self) -> Optional[bool]: ...
  80. def __aiter__(self) -> AsyncIterator[Any]: ...
  81. class AsyncAdapt_dbapi_module:
  82. if TYPE_CHECKING:
  83. Error = DBAPIModule.Error
  84. OperationalError = DBAPIModule.OperationalError
  85. InterfaceError = DBAPIModule.InterfaceError
  86. IntegrityError = DBAPIModule.IntegrityError
  87. def __getattr__(self, key: str) -> Any: ...
  88. class AsyncAdapt_dbapi_cursor:
  89. server_side = False
  90. __slots__ = (
  91. "_adapt_connection",
  92. "_connection",
  93. "await_",
  94. "_cursor",
  95. "_rows",
  96. "_soft_closed_memoized",
  97. )
  98. _awaitable_cursor_close: bool = True
  99. _cursor: AsyncIODBAPICursor
  100. _adapt_connection: AsyncAdapt_dbapi_connection
  101. _connection: AsyncIODBAPIConnection
  102. _rows: Deque[Any]
  103. def __init__(self, adapt_connection: AsyncAdapt_dbapi_connection):
  104. self._adapt_connection = adapt_connection
  105. self._connection = adapt_connection._connection
  106. self.await_ = adapt_connection.await_
  107. cursor = self._make_new_cursor(self._connection)
  108. self._cursor = self._aenter_cursor(cursor)
  109. self._soft_closed_memoized = EMPTY_DICT
  110. if not self.server_side:
  111. self._rows = collections.deque()
  112. def _aenter_cursor(self, cursor: AsyncIODBAPICursor) -> AsyncIODBAPICursor:
  113. return self.await_(cursor.__aenter__()) # type: ignore[no-any-return]
  114. def _make_new_cursor(
  115. self, connection: AsyncIODBAPIConnection
  116. ) -> AsyncIODBAPICursor:
  117. return connection.cursor()
  118. @property
  119. def description(self) -> Optional[_DBAPICursorDescription]:
  120. if "description" in self._soft_closed_memoized:
  121. return self._soft_closed_memoized["description"] # type: ignore[no-any-return] # noqa: E501
  122. return self._cursor.description
  123. @property
  124. def rowcount(self) -> int:
  125. return self._cursor.rowcount
  126. @property
  127. def arraysize(self) -> int:
  128. return self._cursor.arraysize
  129. @arraysize.setter
  130. def arraysize(self, value: int) -> None:
  131. self._cursor.arraysize = value
  132. @property
  133. def lastrowid(self) -> int:
  134. return self._cursor.lastrowid
  135. async def _async_soft_close(self) -> None:
  136. """close the cursor but keep the results pending, and memoize the
  137. description.
  138. .. versionadded:: 2.0.44
  139. """
  140. if not self._awaitable_cursor_close or self.server_side:
  141. return
  142. self._soft_closed_memoized = self._soft_closed_memoized.union(
  143. {
  144. "description": self._cursor.description,
  145. }
  146. )
  147. await self._cursor.close()
  148. def close(self) -> None:
  149. self._rows.clear()
  150. # updated as of 2.0.44
  151. # try to "close" the cursor based on what we know about the driver
  152. # and if we are able to. otherwise, hope that the asyncio
  153. # extension called _async_soft_close() if the cursor is going into
  154. # a sync context
  155. if self._cursor is None or bool(self._soft_closed_memoized):
  156. return
  157. if not self._awaitable_cursor_close:
  158. self._cursor.close() # type: ignore[unused-coroutine]
  159. elif in_greenlet():
  160. self.await_(self._cursor.close())
  161. def execute(
  162. self,
  163. operation: Any,
  164. parameters: Optional[_DBAPISingleExecuteParams] = None,
  165. ) -> Any:
  166. try:
  167. return self.await_(self._execute_async(operation, parameters))
  168. except Exception as error:
  169. self._adapt_connection._handle_exception(error)
  170. def executemany(
  171. self,
  172. operation: Any,
  173. seq_of_parameters: _DBAPIMultiExecuteParams,
  174. ) -> Any:
  175. try:
  176. return self.await_(
  177. self._executemany_async(operation, seq_of_parameters)
  178. )
  179. except Exception as error:
  180. self._adapt_connection._handle_exception(error)
  181. async def _execute_async(
  182. self, operation: Any, parameters: Optional[_DBAPISingleExecuteParams]
  183. ) -> Any:
  184. async with self._adapt_connection._execute_mutex:
  185. if parameters is None:
  186. result = await self._cursor.execute(operation)
  187. else:
  188. result = await self._cursor.execute(operation, parameters)
  189. if self._cursor.description and not self.server_side:
  190. self._rows = collections.deque(await self._cursor.fetchall())
  191. return result
  192. async def _executemany_async(
  193. self,
  194. operation: Any,
  195. seq_of_parameters: _DBAPIMultiExecuteParams,
  196. ) -> Any:
  197. async with self._adapt_connection._execute_mutex:
  198. return await self._cursor.executemany(operation, seq_of_parameters)
  199. def nextset(self) -> None:
  200. self.await_(self._cursor.nextset())
  201. if self._cursor.description and not self.server_side:
  202. self._rows = collections.deque(
  203. self.await_(self._cursor.fetchall())
  204. )
  205. def setinputsizes(self, *inputsizes: Any) -> None:
  206. # NOTE: this is overridden in aioodbc due to
  207. # see https://github.com/aio-libs/aioodbc/issues/451
  208. # right now
  209. return self.await_(self._cursor.setinputsizes(*inputsizes))
  210. def __enter__(self) -> Self:
  211. return self
  212. def __exit__(self, type_: Any, value: Any, traceback: Any) -> None:
  213. self.close()
  214. def __iter__(self) -> Iterator[Any]:
  215. while self._rows:
  216. yield self._rows.popleft()
  217. def fetchone(self) -> Optional[Any]:
  218. if self._rows:
  219. return self._rows.popleft()
  220. else:
  221. return None
  222. def fetchmany(self, size: Optional[int] = None) -> Sequence[Any]:
  223. if size is None:
  224. size = self.arraysize
  225. rr = self._rows
  226. return [rr.popleft() for _ in range(min(size, len(rr)))]
  227. def fetchall(self) -> Sequence[Any]:
  228. retval = list(self._rows)
  229. self._rows.clear()
  230. return retval
  231. class AsyncAdapt_dbapi_ss_cursor(AsyncAdapt_dbapi_cursor):
  232. __slots__ = ()
  233. server_side = True
  234. def close(self) -> None:
  235. if self._cursor is not None:
  236. self.await_(self._cursor.close())
  237. self._cursor = None # type: ignore
  238. def fetchone(self) -> Optional[Any]:
  239. return self.await_(self._cursor.fetchone())
  240. def fetchmany(self, size: Optional[int] = None) -> Any:
  241. return self.await_(self._cursor.fetchmany(size=size))
  242. def fetchall(self) -> Sequence[Any]:
  243. return self.await_(self._cursor.fetchall())
  244. def __iter__(self) -> Iterator[Any]:
  245. iterator = self._cursor.__aiter__()
  246. while True:
  247. try:
  248. yield self.await_(iterator.__anext__())
  249. except StopAsyncIteration:
  250. break
  251. class AsyncAdapt_dbapi_connection(AdaptedConnection):
  252. _cursor_cls = AsyncAdapt_dbapi_cursor
  253. _ss_cursor_cls = AsyncAdapt_dbapi_ss_cursor
  254. await_ = staticmethod(await_only)
  255. __slots__ = ("dbapi", "_execute_mutex")
  256. _connection: AsyncIODBAPIConnection
  257. def __init__(self, dbapi: Any, connection: AsyncIODBAPIConnection):
  258. self.dbapi = dbapi
  259. self._connection = connection
  260. self._execute_mutex = asyncio.Lock()
  261. def cursor(self, server_side: bool = False) -> AsyncAdapt_dbapi_cursor:
  262. if server_side:
  263. return self._ss_cursor_cls(self)
  264. else:
  265. return self._cursor_cls(self)
  266. def execute(
  267. self,
  268. operation: Any,
  269. parameters: Optional[_DBAPISingleExecuteParams] = None,
  270. ) -> Any:
  271. """lots of DBAPIs seem to provide this, so include it"""
  272. cursor = self.cursor()
  273. cursor.execute(operation, parameters)
  274. return cursor
  275. def _handle_exception(self, error: Exception) -> NoReturn:
  276. exc_info = sys.exc_info()
  277. raise error.with_traceback(exc_info[2])
  278. def rollback(self) -> None:
  279. try:
  280. self.await_(self._connection.rollback())
  281. except Exception as error:
  282. self._handle_exception(error)
  283. def commit(self) -> None:
  284. try:
  285. self.await_(self._connection.commit())
  286. except Exception as error:
  287. self._handle_exception(error)
  288. def close(self) -> None:
  289. self.await_(self._connection.close())
  290. class AsyncAdaptFallback_dbapi_connection(AsyncAdapt_dbapi_connection):
  291. __slots__ = ()
  292. await_ = staticmethod(await_fallback)
  293. class AsyncAdapt_terminate:
  294. """Mixin for a AsyncAdapt_dbapi_connection to add terminate support."""
  295. __slots__ = ()
  296. def terminate(self) -> None:
  297. if in_greenlet():
  298. # in a greenlet; this is the connection was invalidated case.
  299. try:
  300. # try to gracefully close; see #10717
  301. self.await_(asyncio.shield(self._terminate_graceful_close())) # type: ignore[attr-defined] # noqa: E501
  302. except self._terminate_handled_exceptions() as e:
  303. # in the case where we are recycling an old connection
  304. # that may have already been disconnected, close() will
  305. # fail. In this case, terminate
  306. # the connection without any further waiting.
  307. # see issue #8419
  308. self._terminate_force_close()
  309. if isinstance(e, asyncio.CancelledError):
  310. # re-raise CancelledError if we were cancelled
  311. raise
  312. else:
  313. # not in a greenlet; this is the gc cleanup case
  314. self._terminate_force_close()
  315. def _terminate_handled_exceptions(self) -> Tuple[Type[BaseException], ...]:
  316. """Returns the exceptions that should be handled when
  317. calling _graceful_close.
  318. """
  319. return (asyncio.TimeoutError, asyncio.CancelledError, OSError)
  320. async def _terminate_graceful_close(self) -> None:
  321. """Try to close connection gracefully"""
  322. raise NotImplementedError
  323. def _terminate_force_close(self) -> None:
  324. """Terminate the connection"""
  325. raise NotImplementedError