From 54c36c08be5d49402adcd482693be33342355aab Mon Sep 17 00:00:00 2001 From: nocturn9x Date: Sat, 21 Mar 2020 10:29:22 +0000 Subject: [PATCH] Fixed the cancellation mechanism --- experiment.py | 11 +++++------ giambio/core.py | 5 ++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/experiment.py b/experiment.py index 9963755..c53f178 100644 --- a/experiment.py +++ b/experiment.py @@ -5,12 +5,12 @@ loop = giambio.EventLoop() """ -What works and what does not (21st March 2020 10:33 AM) +What works and what does not (21st March 2020 11:22 AM) - Run tasks concurrently: V - Join mechanism: V - Sleep mechanism: V -- Cancellation mechanism: X Note: giambio.exceptions.CancelledError is raised inside the parent task instead of the child one, probably related to some f*ck ups with the value of EventLoop.running, need to investigate +- Cancellation mechanism: V - Exception propagation: V - Concurrent I/O: X Note: I/O would work only when a task is joined (weird) - Return values of coroutines: X Note: Return values ARE actually stored in task objects properly, but are messed up later when joining tasks @@ -26,7 +26,7 @@ async def countdown(n): await giambio.sleep(1) print("Countdown over") return "Count DOWN over" - except CancelledError: + except giambio.exceptions.CancelledError: print("countdown cancelled!") async def count(stop, step=1): @@ -43,8 +43,7 @@ async def main(): print("Spawning countdown immediately, scheduling count for 2 secs from now") task = loop.spawn(countdown(8)) task1 = loop.schedule(count(12, 2), 2) - await task.join() - await task1.join() - print("All done") + await giambio.sleep(2) # Wait before cancelling + await task.cancel() # Cancel the task loop.start(main) diff --git a/giambio/core.py b/giambio/core.py index f1a2d7e..583a4b6 100644 --- a/giambio/core.py +++ b/giambio/core.py @@ -51,9 +51,8 @@ class EventLoop: except StopIteration as e: # TODO: Fix this return mechanism, it looks like the return value of the child task gets "replaced" by None at some point self.running.ret_value = e.args[0] if e.args else None # Saves the return value self.to_run.extend(self.joined.pop(self.running, ())) # Reschedules the parent task - except CancelledError: - self.running.cancelled = True # Update the coroutine status - raise # TODO: Fix this, removing this raises RuntimeError: cannot reuse already awaited coroutine + except RuntimeError: + self.running.cancelled = True except Exception as has_raised: self.to_run.extend(self.joined.pop(self.running, ())) # Reschedules the parent task if self.running.joined: # Let the join function handle the hassle of propagating the error