From f4ac001f49118e1d9508aeec44bff3fce0eba19b Mon Sep 17 00:00:00 2001 From: Nocturn9x Date: Thu, 1 Jun 2023 20:33:09 +0200 Subject: [PATCH] Minor changes --- structio/sync.py | 4 ++-- tests/self_cancel.py | 1 + tests/shields.py | 3 +++ tests/task_handling.py | 12 ++++++------ 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/structio/sync.py b/structio/sync.py index c3aa127..1fd801b 100644 --- a/structio/sync.py +++ b/structio/sync.py @@ -74,7 +74,7 @@ class Queue: # Stores event objects for tasks wanting to # put items on the queue self.putters: deque[Event] = deque() - self.container: deque[Event] = deque() + self.container: deque = deque() def __len__(self): """ @@ -84,7 +84,7 @@ class Queue: return len(self.container) def __repr__(self) -> str: - return f"{type(self).__name__}({f', '.join(map(str, self.container))})" + return f"{self.__class__.__name__}({f', '.join(map(str, self.container))})" async def __aiter__(self): """ diff --git a/tests/self_cancel.py b/tests/self_cancel.py index b1e814a..1c7bc05 100644 --- a/tests/self_cancel.py +++ b/tests/self_cancel.py @@ -41,5 +41,6 @@ async def main_nested(n, o, p): print(f"[main] Parent exited in {structio.clock() - t:.2f} seconds") +# Should take about 4 seconds structio.run(main_simple, 5, 2, 2) structio.run(main_nested, 5, 2, 2) diff --git a/tests/shields.py b/tests/shields.py index bba24df..dc2aba7 100644 --- a/tests/shields.py +++ b/tests/shields.py @@ -34,6 +34,9 @@ async def main_cancel(i, j): p.spawn(canceller, s, i) await structio.sleep(j) print("[main] Canceling scope") + # Shields only protect from indirect cancellations + # coming from outer scopes: they are still cancellable + # explicitly! s.cancel() print(f"[main] Exited in {structio.clock() - t:.2f} seconds") diff --git a/tests/task_handling.py b/tests/task_handling.py index 40a4735..fb2dec6 100644 --- a/tests/task_handling.py +++ b/tests/task_handling.py @@ -16,7 +16,7 @@ async def main_cancel(i): print(f"[main] Exited in {structio.clock() - t:.2f} seconds") -async def main_wait_successful(): +async def main_wait_successful(i): print("[main] Parent is alive, spawning (and explicitly waiting for) child") t = structio.clock() async with structio.create_pool() as pool: @@ -27,17 +27,17 @@ async def main_wait_successful(): # in this example we could've just awaited the coroutine directly, # so it's a bad show for the feature, but you could theoretically # pass the object around somewhere else and do the awaiting there - print(f"[main] Child has returned: {await pool.spawn(successful, 'test', 5)}") + print(f"[main] Child has returned: {await pool.spawn(successful, 'test', i)}") print(f"[main] Exited in {structio.clock() - t:.2f} seconds") -async def main_wait_failing(): +async def main_wait_failing(i): print("[main] Parent is alive, spawning (and explicitly waiting for) child") t = structio.clock() try: async with structio.create_pool() as pool: # This never completes - await pool.spawn(failing, "test", 5) + await pool.spawn(failing, "test", i) print("This is never executed") except TypeError: print(f"[main] TypeError caught!") @@ -46,6 +46,6 @@ async def main_wait_failing(): # Total time should be about 15s structio.run(main_cancel, 5) -structio.run(main_wait_successful) -structio.run(main_wait_failing) +structio.run(main_wait_successful, 5) +structio.run(main_wait_failing, 5)