summaryrefslogtreecommitdiff
path: root/kvmd/aiotools.py
diff options
context:
space:
mode:
authorMaxim Devaev <[email protected]>2022-07-17 15:27:02 +0300
committerMaxim Devaev <[email protected]>2022-07-17 15:27:02 +0300
commitbe8032893eacf3242431c44ea34003b0458863af (patch)
tree61026dd38b05a6062e9052eabeaf9d4d64ff8d47 /kvmd/aiotools.py
parentb7e220b4c5d0785427f59cb5f0756df288e7a075 (diff)
improved aiotools.run()
Diffstat (limited to 'kvmd/aiotools.py')
-rw-r--r--kvmd/aiotools.py49
1 files changed, 35 insertions, 14 deletions
diff --git a/kvmd/aiotools.py b/kvmd/aiotools.py
index d085cf89..6895421c 100644
--- a/kvmd/aiotools.py
+++ b/kvmd/aiotools.py
@@ -44,6 +44,41 @@ from .logging import get_logger
# =====
+def run(coro: Coroutine, final: Optional[Coroutine]=None) -> None:
+ # https://github.com/aio-libs/aiohttp/blob/a1d4dac1d/aiohttp/web.py#L515
+
+ def sigint_handler() -> None:
+ raise KeyboardInterrupt()
+
+ def sigterm_handler() -> None:
+ raise SystemExit()
+
+ loop = asyncio.get_event_loop()
+ loop.add_signal_handler(signal.SIGINT, sigint_handler)
+ loop.add_signal_handler(signal.SIGTERM, sigterm_handler)
+
+ main_task = loop.create_task(coro)
+ try:
+ loop.run_until_complete(main_task)
+ except (SystemExit, KeyboardInterrupt):
+ pass
+ finally:
+ main_task.cancel()
+ loop.run_until_complete(asyncio.gather(main_task, return_exceptions=True))
+
+ if final is not None:
+ loop.run_until_complete(final)
+
+ tasks = asyncio.all_tasks(loop)
+ for task in tasks:
+ task.cancel()
+ loop.run_until_complete(asyncio.gather(*tasks, return_exceptions=True))
+
+ loop.run_until_complete(loop.shutdown_asyncgens())
+ loop.close()
+
+
+# =====
_FunctionT = TypeVar("_FunctionT", bound=Callable[..., Any])
_RetvalT = TypeVar("_RetvalT")
@@ -152,20 +187,6 @@ async def close_writer(writer: asyncio.StreamWriter) -> bool:
# =====
-def run(coro: Coroutine) -> None:
- def sigint_handler() -> None:
- raise KeyboardInterrupt()
-
- def sigterm_handler() -> None:
- raise SystemExit()
-
- loop = asyncio.get_event_loop()
- loop.add_signal_handler(signal.SIGINT, sigint_handler)
- loop.add_signal_handler(signal.SIGTERM, sigterm_handler)
- loop.run_until_complete(coro)
-
-
-# =====
class AioNotifier:
def __init__(self) -> None:
self.__queue: "asyncio.Queue[None]" = asyncio.Queue()