graceful sigint

This commit is contained in:
xenia 2021-05-12 18:02:01 -04:00
parent 801f9eb1f7
commit c760bf2fb3
1 changed files with 9 additions and 2 deletions

View File

@ -3,6 +3,7 @@ import asyncio
import contextlib
import errno
import re
import signal
import sys
import termios
import tty
@ -124,8 +125,14 @@ async def megacom(tty: str, baud: int, mode: str, logfile: Optional[str]) -> Non
loop = asyncio.get_event_loop()
keycodes = KeycodeHandler()
return await megacom_main(stdin, stdout, tty, baud, bytesize, parity, stopbits, loop, keycodes,
log)
loop.add_signal_handler(signal.SIGINT, lambda: keycodes.exit_flag.set())
try:
return await megacom_main(stdin, stdout, tty, baud, bytesize, parity, stopbits, loop,
keycodes, log)
finally:
loop.remove_signal_handler(signal.SIGINT)
async def megacom_main(stdin: asyncio.StreamReader, stdout: asyncio.StreamWriter, tty: str,