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
- 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)

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
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