From 13673aa7e2d6906e9021bffd906099fbfa3b7a5d Mon Sep 17 00:00:00 2001 From: Gabriel Matte Date: Thu, 16 Feb 2023 17:37:47 -0300 Subject: [PATCH] Simple versioning conditions for the Connection pool and making sure that the examples use the correct version of the library. --- aioodbc/pool.py | 87 ++++++++++++++++++++-------- examples/example_complex_queries.py | 3 + examples/example_context_managers.py | 3 + examples/example_pool.py | 3 + examples/example_simple.py | 3 + 5 files changed, 74 insertions(+), 25 deletions(-) diff --git a/aioodbc/pool.py b/aioodbc/pool.py index cb9a061..2824ae7 100644 --- a/aioodbc/pool.py +++ b/aioodbc/pool.py @@ -6,6 +6,7 @@ from .connection import connect from .utils import _PoolContextManager, _PoolConnectionContextManager +import sys __all__ = ['create_pool', 'Pool'] @@ -25,8 +26,12 @@ async def _create_pool(minsize=10, maxsize=10, echo=False, loop=None, pool = Pool(minsize=minsize, maxsize=maxsize, echo=echo, loop=loop, pool_recycle=pool_recycle, **kwargs) if minsize > 0: - with (await pool._cond): - await pool._fill_free_pool(False) + if sys.version_info < (3,11): + with (await pool._cond): + await pool._fill_free_pool(False) + else: + async with pool._cond: + await pool._fill_free_pool(False) return pool @@ -44,7 +49,10 @@ def __init__(self, minsize, maxsize, echo, loop, pool_recycle, **kwargs): self._acquiring = 0 self._recycle = pool_recycle self._free = collections.deque(maxlen=maxsize) - self._cond = asyncio.Condition(loop=loop) + if sys.version_info < (3,11): + self._cond = asyncio.Condition(loop=loop) + else: + self._cond = asyncio.Condition() self._used = set() self._closing = False self._closed = False @@ -76,11 +84,18 @@ def closed(self): async def clear(self): """Close all free connections in pool.""" - with (await self._cond): - while self._free: - conn = self._free.popleft() - await conn.close() - self._cond.notify() + if sys.version_info < (3,11): + with (await self._cond): + while self._free: + conn = self._free.popleft() + await conn.close() + self._cond.notify() + else: + async with self._cond: + while self._free: + conn = self._free.popleft() + await conn.close() + self._cond.notify() def close(self): """Close pool. @@ -104,10 +119,15 @@ async def wait_closed(self): while self._free: conn = self._free.popleft() await conn.close() - - with (await self._cond): - while self.size > self.freesize: - await self._cond.wait() + + if sys.version_info < (3,11): + with (await self._cond): + while self.size > self.freesize: + await self._cond.wait() + else: + async with self._cond: + while self.size > self.freesize: + await self._cond.wait() self._closed = True @@ -119,17 +139,30 @@ def acquire(self): async def _acquire(self): if self._closing: raise RuntimeError("Cannot acquire connection after closing pool") - with (await self._cond): - while True: - await self._fill_free_pool(True) - if self._free: - conn = self._free.popleft() - assert not conn.closed, conn - assert conn not in self._used, (conn, self._used) - self._used.add(conn) - return conn - else: - await self._cond.wait() + if sys.version_info < (3,11): + with (await self._cond): + while True: + await self._fill_free_pool(True) + if self._free: + conn = self._free.popleft() + assert not conn.closed, conn + assert conn not in self._used, (conn, self._used) + self._used.add(conn) + return conn + else: + await self._cond.wait() + else: + async with self._cond: + while True: + await self._fill_free_pool(True) + if self._free: + conn = self._free.popleft() + assert not conn.closed, conn + assert conn not in self._used, (conn, self._used) + self._used.add(conn) + return conn + else: + await self._cond.wait() async def _fill_free_pool(self, override_min): n, free = 0, len(self._free) @@ -168,8 +201,12 @@ async def _fill_free_pool(self, override_min): self._acquiring -= 1 async def _wakeup(self): - with (await self._cond): - self._cond.notify() + if sys.version_info < (3,11): + with (await self._cond): + self._cond.notify() + else: + async with self._cond: + self._cond.notify() async def release(self, conn): """Release free connection back to the connection pool. diff --git a/examples/example_complex_queries.py b/examples/example_complex_queries.py index cf995ae..fd67dc5 100644 --- a/examples/example_complex_queries.py +++ b/examples/example_complex_queries.py @@ -1,4 +1,7 @@ import asyncio +import sys +import os +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) import aioodbc from functools import partial diff --git a/examples/example_context_managers.py b/examples/example_context_managers.py index 6703f77..6ddb195 100644 --- a/examples/example_context_managers.py +++ b/examples/example_context_managers.py @@ -1,4 +1,7 @@ import asyncio +import sys +import os +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) import aioodbc diff --git a/examples/example_pool.py b/examples/example_pool.py index f070325..53475be 100644 --- a/examples/example_pool.py +++ b/examples/example_pool.py @@ -1,4 +1,7 @@ import asyncio +import sys +import os +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) import aioodbc diff --git a/examples/example_simple.py b/examples/example_simple.py index 7438d94..e9f2c0a 100644 --- a/examples/example_simple.py +++ b/examples/example_simple.py @@ -1,4 +1,7 @@ import asyncio +import sys +import os +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) import aioodbc