Fixed the TaskManager exception handling

This commit is contained in:
nocturn9x 2020-04-04 17:14:04 +00:00
parent f890afacd1
commit 499ea4da85
3 changed files with 11 additions and 5 deletions

View File

@ -30,7 +30,7 @@ async def countdown(n):
return "Count DOWN over" return "Count DOWN over"
except giambio.CancelledError: except giambio.CancelledError:
print("countdown cancelled!") print("countdown cancelled!")
raise Exception("Oh no!") #TODO Propagate this raise Exception("Oh no!") # TODO Propagate this
async def count(stop, step=1): async def count(stop, step=1):
try: try:

View File

@ -4,7 +4,7 @@ from collections import deque, defaultdict
from selectors import DefaultSelector, EVENT_READ, EVENT_WRITE from selectors import DefaultSelector, EVENT_READ, EVENT_WRITE
from heapq import heappush, heappop from heapq import heappush, heappop
import socket import socket
from .exceptions import AlreadyJoinedError, CancelledError from .exceptions import AlreadyJoinedError, CancelledError, GiambioError
from timeit import default_timer from timeit import default_timer
from time import sleep as wait from time import sleep as wait
from .socket import AsyncSocket, WantRead, WantWrite from .socket import AsyncSocket, WantRead, WantWrite
@ -29,6 +29,7 @@ class EventLoop:
self.joined = defaultdict(list) # Tasks that want to join self.joined = defaultdict(list) # Tasks that want to join
self.clock = default_timer # Monotonic clock to keep track of elapsed time self.clock = default_timer # Monotonic clock to keep track of elapsed time
self.sequence = 0 # To avoid TypeError in the (unlikely) event of two task with the same deadline we use a unique and incremental integer pushed to the queue together with the deadline and the function itself self.sequence = 0 # To avoid TypeError in the (unlikely) event of two task with the same deadline we use a unique and incremental integer pushed to the queue together with the deadline and the function itself
self._exiting = False
def loop(self): def loop(self):
"""Main event loop for giambio""" """Main event loop for giambio"""
@ -63,9 +64,12 @@ class EventLoop:
self.running.execution = "CANCELLED" self.running.execution = "CANCELLED"
self.to_run.extend(self.joined.pop(self.running, ())) self.to_run.extend(self.joined.pop(self.running, ()))
except Exception as err: except Exception as err:
self.running.execution = "ERRORED" if not self._exiting:
self.running.result = Result(None, err) self.running.execution = "ERRORED"
self.to_run.extend(self.joined.pop(self.running, ())) # Reschedules the parent task self.running.result = Result(None, err)
self.to_run.extend(self.joined.pop(self.running, ())) # Reschedules the parent task
else:
raise
except KeyboardInterrupt: except KeyboardInterrupt:
self.running.throw(KeyboardInterrupt) self.running.throw(KeyboardInterrupt)

View File

@ -17,8 +17,10 @@ class TaskManager:
async def _cancel_and_raise(self, exc): async def _cancel_and_raise(self, exc):
self.loop._exiting = True # Tells the loop not to catch all exceptions
for task in self.tasks: for task in self.tasks:
await task.cancel() await task.cancel()
raise exc
async def __aenter__(self): async def __aenter__(self):
return self return self