signals handling

This commit is contained in:
Devaev Maxim
2021-05-25 23:26:13 +03:00
parent 98c3956994
commit 6ce07208a1
2 changed files with 40 additions and 28 deletions

View File

@@ -21,6 +21,7 @@
import asyncio
import signal
import functools
import types
@@ -101,6 +102,20 @@ async def close_writer(writer: asyncio.StreamWriter) -> bool:
return (not closing)
# =====
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: