From 2e202911f0f0b23e8f5a54cf7cdf2937281c7bcd Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 3 Jan 2022 16:03:48 +0100 Subject: [PATCH] AsyncConnectionPool.open() made async. This makes it symmetrical with close(). However it doesn't really do any async work as it's awkward to call it from init. Something we might do, if that will be needed, could be to start the scheduler only and use it to schedule immediately a call to an async _open(). In the future, an anyio-based pool might instead disallow open=True on init. See https://github.com/psycopg/psycopg/pull/151 for some discussion about the topic. --- psycopg_pool/psycopg_pool/pool_async.py | 10 +++++++--- tests/pool/test_pool_async.py | 6 +++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/psycopg_pool/psycopg_pool/pool_async.py b/psycopg_pool/psycopg_pool/pool_async.py index 46a6b2f2b..daa432678 100644 --- a/psycopg_pool/psycopg_pool/pool_async.py +++ b/psycopg_pool/psycopg_pool/pool_async.py @@ -65,7 +65,7 @@ class AsyncConnectionPool(BasePool[AsyncConnection[Any]]): super().__init__(conninfo, **kwargs) if open: - self.open() + self._open() async def wait(self, timeout: float = 30.0) -> None: async with self._lock: @@ -192,7 +192,11 @@ class AsyncConnectionPool(BasePool[AsyncConnection[Any]]): else: await self._return_connection(conn) - def open(self) -> None: + async def open(self) -> None: + async with self._lock: + self._open() + + def _open(self) -> None: if not self._closed: return @@ -279,7 +283,7 @@ class AsyncConnectionPool(BasePool[AsyncConnection[Any]]): ) async def __aenter__(self) -> "AsyncConnectionPool": - self.open() + await self.open() return self async def __aexit__( diff --git a/tests/pool/test_pool_async.py b/tests/pool/test_pool_async.py index ea2868dcc..876a1e8cd 100644 --- a/tests/pool/test_pool_async.py +++ b/tests/pool/test_pool_async.py @@ -683,7 +683,7 @@ async def test_open_explicit(dsn): async with p.connection(): pass - p.open() + await p.open() try: assert not p.closed @@ -713,7 +713,7 @@ async def test_open_no_op(dsn): p = pool.AsyncConnectionPool(dsn) try: assert not p.closed - p.open() + await p.open() assert not p.closed async with p.connection() as conn: @@ -732,7 +732,7 @@ async def test_reopen(dsn): assert p._sched_runner is None with pytest.raises(OperationalError, match="cannot be reused"): - p.open() + await p.open() @pytest.mark.slow -- 2.47.2