Added some TODOs and tried a solution for the cancel() method (and failed)

This commit is contained in:
nocturn9x 2020-03-23 19:49:12 +00:00
parent 3ad0d46cda
commit 8d9b874228
2 changed files with 22 additions and 14 deletions

View File

@ -28,21 +28,25 @@ async def countdown(n):
print("countdown cancelled!") print("countdown cancelled!")
async def count(stop, step=1): async def count(stop, step=1):
x = 0 try:
while x < stop: x = 0
print(f"Up {x}") while x < stop:
x += step print(f"Up {x}")
await giambio.sleep(step) x += step
print("Countup over") await giambio.sleep(step)
return "Count UP over" print("Countup over")
return "Count UP over"
except giambio.exceptions.CancelledError:
print("count cancelled!")
async def main(): async def main():
print("Spawning countdown immediately, scheduling count for 2 secs from now") print("Spawning countdown immediately, scheduling count for 4 secs from now")
task = loop.spawn(countdown(8)) task = loop.spawn(countdown(8))
task1 = loop.schedule(count(8, 2), 2) task1 = loop.schedule(count(8, 2), 4)
await giambio.sleep(0) # Beware! Cancelling a task straight away will propagate the error in the parent
# await task.cancel() # TODO: Fix this to reschedule the parent task properly
result = await task.join() result = await task.join()
result1 = await task1.join() # Joining a scheduled task does not reschedule the parent task result1 = await task1.join()
print(f"countdown returned: {result}\ncount returned: {result1}") print(f"countdown returned: {result}\ncount returned: {result1}")
print("All done") print("All done")

View File

@ -51,6 +51,8 @@ class EventLoop:
except StopIteration as e: except StopIteration as e:
self.running.result = Result(e.args[0] if e.args else None, None) # Saves the return value self.running.result = Result(e.args[0] if e.args else None, 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 RuntimeError:
self.to_run.append(self.running)
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
@ -158,6 +160,7 @@ class Task:
self.joined = False self.joined = False
self.result = None # Updated when the coroutine execution ends self.result = None # Updated when the coroutine execution ends
self.loop = loop # The EventLoop object that spawned the task self.loop = loop # The EventLoop object that spawned the task
self.cancelled = False
def run(self): def run(self):
self.status = True self.status = True
@ -210,9 +213,10 @@ def want_write(sock: socket.socket):
def join(task: Task): def join(task: Task):
"""'Tells' the scheduler that the desired task MUST be awaited for completion""" """'Tells' the scheduler that the desired task MUST be awaited for completion"""
task.joined = True if not task.cancelled:
yield "want_join", task task.joined = True
return task.get_result() yield "want_join", task
return task.get_result()
@types.coroutine @types.coroutine