Fixed the join mechanism and the schedule method

This commit is contained in:
nocturn9x 2020-03-23 18:36:58 +00:00
parent f63d7477ed
commit 3ad0d46cda
2 changed files with 5 additions and 8 deletions

View File

@ -42,8 +42,8 @@ async def main():
task = loop.spawn(countdown(8))
task1 = loop.schedule(count(8, 2), 2)
result = await task.join()
# result1 = await task1.join() # Joining a scheduled task does not reschedule the parent task
print(result, result1)
result1 = await task1.join() # Joining a scheduled task does not reschedule the parent task
print(f"countdown returned: {result}\ncount returned: {result1}")
print("All done")
loop.start(main)

View File

@ -48,7 +48,7 @@ class EventLoop:
try:
method, *args = self.running.run() # Sneaky method call, thanks to David Beazley for this ;)
getattr(self, method)(*args)
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:
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 Exception as has_raised:
@ -58,7 +58,7 @@ class EventLoop:
else: # Let the exception propagate (I'm looking at you asyncIO ;))
raise
except KeyboardInterrupt:
self.running.coroutine.throw(KeyboardInterrupt)
self.running.throw(KeyboardInterrupt)
def spawn(self, coroutine: types.coroutine):
"""Schedules a task for execution, appending it to the call stack"""
@ -161,10 +161,7 @@ class Task:
def run(self):
self.status = True
try:
return self.coroutine.send(None)
except RuntimeError:
print(self.loop.to_run)
return self.coroutine.send(None)
def __repr__(self):
return f"giambio.core.Task({self.coroutine}, {self.status}, {self.joined}, {self.result})"