Fixed the cancellation mechanism

This commit is contained in:
nocturn9x 2020-03-21 10:29:22 +00:00
parent 513d825c2b
commit 54c36c08be
2 changed files with 7 additions and 9 deletions

View File

@ -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 - Run tasks concurrently: V
- Join mechanism: V - Join mechanism: V
- Sleep 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 - Exception propagation: V
- Concurrent I/O: X Note: I/O would work only when a task is joined (weird) - 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 - 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) await giambio.sleep(1)
print("Countdown over") print("Countdown over")
return "Count DOWN over" return "Count DOWN over"
except CancelledError: except giambio.exceptions.CancelledError:
print("countdown cancelled!") print("countdown cancelled!")
async def count(stop, step=1): async def count(stop, step=1):
@ -43,8 +43,7 @@ async def main():
print("Spawning countdown immediately, scheduling count for 2 secs from now") print("Spawning countdown immediately, scheduling count for 2 secs from now")
task = loop.spawn(countdown(8)) task = loop.spawn(countdown(8))
task1 = loop.schedule(count(12, 2), 2) task1 = loop.schedule(count(12, 2), 2)
await task.join() await giambio.sleep(2) # Wait before cancelling
await task1.join() await task.cancel() # Cancel the task
print("All done")
loop.start(main) loop.start(main)

View File

@ -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 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.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 self.to_run.extend(self.joined.pop(self.running, ())) # Reschedules the parent task
except CancelledError: except RuntimeError:
self.running.cancelled = True # Update the coroutine status self.running.cancelled = True
raise # TODO: Fix this, removing this raises RuntimeError: cannot reuse already awaited coroutine
except Exception as has_raised: except Exception as has_raised:
self.to_run.extend(self.joined.pop(self.running, ())) # Reschedules the parent task 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 if self.running.joined: # Let the join function handle the hassle of propagating the error