Some changes and fixes to exceptions behavior

This commit is contained in:
nocturn9x 2021-04-22 12:02:40 +02:00
parent dcd3cae674
commit 941464437d
3 changed files with 11 additions and 7 deletions

View File

@ -504,6 +504,13 @@ class AsyncScheduler:
# pool have finished executing, either # pool have finished executing, either
# by cancellation, an exception # by cancellation, an exception
# or just returned # or just returned
for t in task.joiners:
# Propagate the exception
try:
t.throw(task.exc)
except StopIteration:
# TODO: Need anything else?
task.joiners.remove(t)
self.reschedule_joiners(task) self.reschedule_joiners(task)
def sleep(self, seconds: int or float): def sleep(self, seconds: int or float):

View File

@ -35,10 +35,8 @@ async def main():
pool.spawn(child1) pool.spawn(child1)
print("[main] Children spawned, awaiting completion") print("[main] Children spawned, awaiting completion")
async with giambio.create_pool() as new_pool: async with giambio.create_pool() as new_pool:
# This pool won't be affected from exceptions # This pool will be cancelled by the exception
# in outer pools. This is a guarantee that giambio # in the other pool
# ensures: an exception will only be propagated
# after all enclosed task pools have exited
new_pool.spawn(child2) new_pool.spawn(child2)
new_pool.spawn(child3) new_pool.spawn(child3)
print("[main] 3rd child spawned") print("[main] 3rd child spawned")

View File

@ -50,7 +50,7 @@ async def handler(sock: AsyncSocket, client_address: tuple):
break break
elif data == b"exit\n": elif data == b"exit\n":
await sock.send_all(b"I'm dead dude\n") await sock.send_all(b"I'm dead dude\n")
raise TypeError("Oh, no, I'm gonna die!") raise TypeError("Oh, no, I'm gonna die!") # This kills the entire application!
logging.info(f"Got: {data!r} from {address}") logging.info(f"Got: {data!r} from {address}")
await sock.send_all(b"Got: " + data) await sock.send_all(b"Got: " + data)
logging.info(f"Echoed back {data!r} to {address}") logging.info(f"Echoed back {data!r} to {address}")
@ -58,7 +58,7 @@ async def handler(sock: AsyncSocket, client_address: tuple):
if __name__ == "__main__": if __name__ == "__main__":
port = int(sys.argv[1]) if len(sys.argv) > 1 else 1500 port = int(sys.argv[1]) if len(sys.argv) > 1 else 1501
logging.basicConfig(level=20, format="[%(levelname)s] %(asctime)s %(message)s", datefmt="%d/%m/%Y %p") logging.basicConfig(level=20, format="[%(levelname)s] %(asctime)s %(message)s", datefmt="%d/%m/%Y %p")
try: try:
giambio.run(serve, ("localhost", port), debugger=None) giambio.run(serve, ("localhost", port), debugger=None)
@ -67,4 +67,3 @@ if __name__ == "__main__":
logging.info("Ctrl+C detected, exiting") logging.info("Ctrl+C detected, exiting")
else: else:
logging.error(f"Exiting due to a {type(error).__name__}: {error}") logging.error(f"Exiting due to a {type(error).__name__}: {error}")
raise