]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
AsyncConnectionPool.open() made async.
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 3 Jan 2022 15:03:48 +0000 (16:03 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 3 Jan 2022 15:41:10 +0000 (16:41 +0100)
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
tests/pool/test_pool_async.py

index 46a6b2f2b6b1533c8c6444de571b6b69cf621a8a..daa432678cd54bb76b7e7fcf5922f377a62e06da 100644 (file)
@@ -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__(
index ea2868dcc75b4e016c13a92f7f6b36c82d554be5..876a1e8cd7fccb22292277076fcc90d51939a8fd 100644 (file)
@@ -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