Aug-30-2021, 10:08 PM
I found the solution to my problem. I needed to add a callback function for the two tasks, and return values for each Task.
Otherwise, when an exception is raised in a Task, it will not be seen until the Task goes out-of-scope, which in my case since
the Python server script is running in a run_forever loop, will never go out-of-scope until the server script receives a SIGINT.
In case anyone else has a similar problem, here's the update code snippet:
Otherwise, when an exception is raised in a Task, it will not be seen until the Task goes out-of-scope, which in my case since
the Python server script is running in a run_forever loop, will never go out-of-scope until the server script receives a SIGINT.
In case anyone else has a similar problem, here's the update code snippet:
import asyncio import socket ..... addr = (localhost, 50000) loop = asyncio.get_event_loop() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.setblocking(False) sock.bind(addr) sock.listen(N) def _handle_ahandle_throws(task: asyncio.Task): try: task.result() except asyncio.CancelledError: pass except Exception: logging.exception('Exception raised in ahandle...by task = %r', task) async def sserver(): while True: conn, addr = await loop.sock_accept(sock) sr = SRequests(pool, loop, log) sums_task = loop.create_task(sr.ahandle(conn)) sums_task.add_done_callback(_handle_ahandle_throws) loop.create_task(sserver()) try: loop.run_forever() except KeyboardInterrupt: sserver.close() loop.run_until_complete(sserver.wait_close()) loop.close() sock.shutdown(socket.SHUT_RDWR) sock.close() finally: loop.run_until_complete(loop.shutdown_asyncgens()) loop.close() sock.shutdown(socket.SHUT_RDWR) sock.close() ... class SRequests(asyncio.Protocol, SRequest) def __init__(self, pool, loop, log): self.pool = pool self.loop = loop self.log = log self.setup() return None def _handle_db_task_throws(task: asyncio.Task): try: task.result() except asyncio.CancelledError: pass except Exception: logging.exception('Exception raised by db_task = %r', task) async def getDBConn(self): ... dbConn = DBConnection(...) ... return dbConn def setup(self): ... db_task = self.loop.create_task(self.getDBConn()) db_task.add_done_callback(_handle_db_task_throws) ...