Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple versioning conditions for the Connection pool and making sure that the examples use the correct version of the library. #385

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 62 additions & 25 deletions aioodbc/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from .connection import connect
from .utils import _PoolContextManager, _PoolConnectionContextManager
import sys

__all__ = ['create_pool', 'Pool']

Expand All @@ -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


Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions examples/example_complex_queries.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions examples/example_context_managers.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
3 changes: 3 additions & 0 deletions examples/example_pool.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
3 changes: 3 additions & 0 deletions examples/example_simple.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down