]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Fix Python 3.6 compatibility issues in async pool
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 27 Feb 2021 11:13:17 +0000 (12:13 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 12 Mar 2021 04:07:25 +0000 (05:07 +0100)
psycopg3/psycopg3/pool/async_pool.py

index 528ffa4ade38a21b769d66a62cc01cb3bd936865..381dbcd9b8441417e304e2b47e36a17d58caec5f 100644 (file)
@@ -4,11 +4,11 @@ psycopg3 synchronous connection pool
 
 # Copyright (C) 2021 The Psycopg Team
 
+import sys
 import asyncio
 import logging
 from time import monotonic
 from typing import Any, Awaitable, Callable, Deque, AsyncIterator, Optional
-from contextlib import asynccontextmanager
 from collections import deque
 
 from ..pq import TransactionStatus
@@ -18,6 +18,16 @@ from . import tasks
 from .base import ConnectionAttempt, BasePool
 from .errors import PoolClosed, PoolTimeout
 
+if sys.version_info >= (3, 7):
+    from contextlib import asynccontextmanager
+
+    get_running_loop = asyncio.get_running_loop
+
+else:
+    from .utils.context import asynccontextmanager
+
+    get_running_loop = asyncio.get_event_loop
+
 logger = logging.getLogger(__name__)
 
 
@@ -38,7 +48,7 @@ class AsyncConnectionPool(BasePool[AsyncConnection]):
         # to notify that the pool is full
         self._pool_full_event: Optional[asyncio.Event] = None
 
-        self.loop = asyncio.get_event_loop()
+        self.loop = get_running_loop()
 
         super().__init__(conninfo, **kwargs)
 
@@ -210,7 +220,7 @@ class AsyncConnectionPool(BasePool[AsyncConnection]):
 
         # Wait for the worker threads to terminate
         if timeout > 0:
-            loop = asyncio.get_running_loop()
+            loop = get_running_loop()
             for t in [self._sched_runner] + self._workers:
                 if not t.is_alive():
                     continue