Context manager to obtain a connection from the pool. Return the connection immediately if available, otherwise wait up to *timeout* or `self.timeout` seconds and throw `PoolTimeout` if a connection is not available in time. Upon context exit, return the connection
(self, timeout: float | None = None)
| 206 | |
| 207 | @asynccontextmanager |
| 208 | async def connection(self, timeout: float | None = None) -> AsyncIterator[ACT]: |
| 209 | """Context manager to obtain a connection from the pool. |
| 210 | |
| 211 | Return the connection immediately if available, otherwise wait up to |
| 212 | *timeout* or `self.timeout` seconds and throw `PoolTimeout` if a |
| 213 | connection is not available in time. |
| 214 | |
| 215 | Upon context exit, return the connection to the pool. Apply the normal |
| 216 | :ref:`connection context behaviour <with-connection>` (commit/rollback |
| 217 | the transaction in case of success/error). If the connection is no more |
| 218 | in working state, replace it with a new one. |
| 219 | """ |
| 220 | conn = await self.getconn(timeout=timeout) |
| 221 | try: |
| 222 | t0 = monotonic() |
| 223 | async with conn: |
| 224 | yield conn |
| 225 | finally: |
| 226 | await self.putconn(conn) |
| 227 | t1 = monotonic() |
| 228 | self._stats[self._USAGE_MS] += int(1000.0 * (t1 - t0)) |
| 229 | |
| 230 | async def getconn(self, timeout: float | None = None) -> ACT: |
| 231 | """Obtain a connection from the pool. |