From 3ef26d04ad50a884b3c0e6dced32f47375b90939 Mon Sep 17 00:00:00 2001 From: benadha Date: Sun, 20 Feb 2022 21:34:08 +0700 Subject: Add signal handler --- epy.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'epy.py') diff --git a/epy.py b/epy.py index dd77e54..fb44586 100755 --- a/epy.py +++ b/epy.py @@ -43,6 +43,7 @@ import multiprocessing import os import re import shutil +import signal import sqlite3 import subprocess import sys @@ -3856,6 +3857,17 @@ def preread(stdscr, filepath: str): reader = Reader(screen=stdscr, ebook=ebook, config=config, state=state) + def handle_signal(signum, _): + """ + Method to raise SystemExit based on signal received + to trigger `try-finally` clause + """ + msg = f"[{os.getpid()}] killed" + if signal.Signals(signum) == signal.SIGTERM: + msg = f"[{os.getpid()}] terminated" + sys.exit(msg) + signal.signal(signal.SIGTERM, handle_signal) + try: reader.run_counting_letters() -- cgit