Now exceptions sort of work

This commit is contained in:
nocturn9x 2020-11-12 22:45:00 +01:00
parent 5ff8d4a06e
commit 8b095fbf72
2 changed files with 28 additions and 35 deletions

View File

@ -62,31 +62,23 @@ class AsyncScheduler:
the loop's functionality only trough some fixed entry points, which in turn yield and the loop's functionality only trough some fixed entry points, which in turn yield and
give execution control to the loop itself.""" give execution control to the loop itself."""
while True: try:
if not self.selector.get_map() and not any( while True:
[self.paused, self.tasks, self.event_waiting] if not self.selector.get_map() and not any(
): # If there is nothing to do, just exit [self.paused, self.tasks, self.event_waiting]
break ): # If there is nothing to do, just exit
if not self.tasks: break
if ( if not self.tasks:
self.paused if (
): # If there are no actively running tasks, we try to schedule the asleep ones self.paused
try: ): # If there are no actively running tasks, we try to schedule the asleep ones
self.check_sleeping() self.check_sleeping()
except BaseException as error: if self.selector.get_map():
self.current_task.exc = error
self.reschedule_parent(self.current_task)
if self.selector.get_map():
try:
self.check_io() self.check_io()
except BaseException as error: while self.tasks: # While there are tasks to run
self.current_task.exc = error self.current_task = (
self.reschedule_parent(self.current_task) self.tasks.popleft()
while self.tasks: # While there are tasks to run ) # Sets the currently running task
self.current_task = (
self.tasks.popleft()
) # Sets the currently running task
try:
if self.current_task.status == "cancel": # Deferred cancellation if self.current_task.status == "cancel": # Deferred cancellation
self.current_task.cancelled = True self.current_task.cancelled = True
self.current_task.throw(CancelledError(self.current_task)) self.current_task.throw(CancelledError(self.current_task))
@ -99,16 +91,17 @@ class AsyncScheduler:
) # Sneaky method call, thanks to David Beazley for this ;) ) # Sneaky method call, thanks to David Beazley for this ;)
if self.event_waiting: if self.event_waiting:
self.check_events() self.check_events()
except CancelledError as cancelled: except CancelledError as cancelled:
self.tasks.remove(cancelled.args[0]) # Remove the dead task self.tasks.remove(cancelled.args[0]) # Remove the dead task
self.tasks.append(self.current_task) self.tasks.append(self.current_task)
except StopIteration as e: # Coroutine ends except StopIteration as e: # Coroutine ends
self.current_task.result = e.args[0] if e.args else None self.current_task.result = e.args[0] if e.args else None
self.current_task.finished = True self.current_task.finished = True
self.reschedule_parent(self.current_task) self.reschedule_parent(self.current_task)
except BaseException as error: # Coroutine raised except BaseException as error: # Coroutine raised
self.current_task.exc = error self.current_task.exc = error
self.reschedule_parent(self.current_task) self.reschedule_parent(self.current_task)
self.join(self.current_task)
def check_events(self): def check_events(self):
"""Checks for ready or expired events and triggers them""" """Checks for ready or expired events and triggers them"""

View File

@ -41,5 +41,5 @@ async def echo_handler(sock: AsyncSocket, addr: tuple):
if __name__ == "__main__": if __name__ == "__main__":
try: try:
sched.start(server(("", 25001))) sched.start(server(("", 25001)))
except KeyboardInterrupt: # Exceptions propagate! except giambio.exceptions.GiambioError as wrapper: # Exceptions propagate!
print("Exiting...") print(f"Exiting due to a {type(wrapper.__cause__).__name__}")