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!")
async def count(stop, step=1):
x = 0
while x < stop:
print(f"Up {x}")
x += step
await giambio.sleep(step)
print("Countup over")
return "Count UP over"
try:
x = 0
while x < stop:
print(f"Up {x}")
x += step
await giambio.sleep(step)
print("Countup over")
return "Count UP over"
except giambio.exceptions.CancelledError:
print("count cancelled!")
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))
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()
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("All done")

View File

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