import asyncio from typing import Awaitable async def wrap_done(fn: Awaitable, event: asyncio.Event): """Wrap an awaitable with a event to signal when it's done or an exception is raised.""" try: await fn except Exception as e: # TODO: handle exception print(f"Caught exception: {e}") finally: # Signal the aiter to stop. event.set()