From: Daniele Varrazzo Date: Sat, 27 Feb 2021 11:13:17 +0000 (+0100) Subject: Fix Python 3.6 compatibility issues in async pool X-Git-Tag: 3.0.dev0~87^2~41 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3d9ca212dd1b351f7baa6eac3db73895f5a813df;p=thirdparty%2Fpsycopg.git Fix Python 3.6 compatibility issues in async pool --- diff --git a/psycopg3/psycopg3/pool/async_pool.py b/psycopg3/psycopg3/pool/async_pool.py index 528ffa4ad..381dbcd9b 100644 --- a/psycopg3/psycopg3/pool/async_pool.py +++ b/psycopg3/psycopg3/pool/async_pool.py @@ -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