diff --git a/satella/__init__.py b/satella/__init__.py index 41fe75710356bbeac00eac9f06dc61a8ba65e079..1e2c5e419af0411134f297c9103b8bc4ac684a9e 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.19.0rc2' +__version__ = '2.19.0rc3' diff --git a/satella/coding/resources/cp_manager.py b/satella/coding/resources/cp_manager.py index 72303fa3e4b2c33f87276b0eb823bad7922fc87b..24b75e249139605d13ca486e9d0ba1ccddc39601 100644 --- a/satella/coding/resources/cp_manager.py +++ b/satella/coding/resources/cp_manager.py @@ -34,8 +34,8 @@ class CPManager(Monitor, Closeable, tp.Generic[T], metaclass=abc.ABCMeta): """ def __init__(self, max_number: int, max_cycle_no: int): - super().__init__() Closeable.__init__(self) + Monitor.__init__(self) if sys.implementation.name != 'cpython': warnings.warn(f'This may run bad on {sys.implementation.name}', RuntimeWarning) self.connections = queue.Queue(max_number) @@ -43,29 +43,36 @@ class CPManager(Monitor, Closeable, tp.Generic[T], metaclass=abc.ABCMeta): self.max_number = max_number self.max_cycle_no = max_cycle_no self.id_to_times = {} # type: tp.Dict[int, int] + self.terminating = False - def close(self) -> bool: + def close(self) -> None: if super().close(): - while self.spawned_connections: + self.terminating = True + while self.spawned_connections > 0: self.teardown_connection(self.connections.get()) self.spawned_connections -= 1 def acquire_connection(self) -> T: """ Either acquire a new connection, or just establish it in the background + + :return: a new connection: + :raises RuntimeError: CPManager is terminating! """ + if self.terminating: + raise RuntimeError('CPManager is terminating') try: conn = self.connections.get(False) except queue.Empty: while True: with silence_excs(queue.Empty), Monitor.acquire(self): - if self.spawned_connections == self.max_number: + if self.spawned_connections >= self.max_number: conn = self.connections.get(False, 5) break elif self.spawned_connections < self.max_number: conn = self.create_connection() - self.connections.put(conn) self.spawned_connections += 1 + self.connections.put(conn) break obj_id = id(conn) try: diff --git a/tests/test_coding/test_resources.py b/tests/test_coding/test_resources.py index 73008b3f617b0b925bc9a9f4ec35d2192b389b5c..24182f90da83c7c5f1d0a22e521a892178737089 100644 --- a/tests/test_coding/test_resources.py +++ b/tests/test_coding/test_resources.py @@ -37,5 +37,4 @@ class TestResources(unittest.TestCase): while conns: cp.release_connection(conns.pop()) - del cp