Python script to test interrupt handling by signal.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/local/bin/python3.6 | |
""" | |
Test interrupt handling by signal | |
""" | |
import datetime | |
import os | |
import signal | |
import sys | |
import time | |
import traceback | |
class TestSignal: | |
def __init__(self): | |
signal.signal(signal.SIGHUP, self.__handler) | |
signal.signal(signal.SIGINT, self.__handler) | |
signal.signal(signal.SIGQUIT, self.__handler) | |
signal.signal(signal.SIGTERM, self.__handler) | |
def exec(self): | |
try: | |
while(True): | |
print(datetime.datetime.now()) | |
time.sleep(5) | |
except Exception as e: | |
raise | |
def __handler(self, signum, frame): | |
try: | |
signames = {1: "HUP", 2: "INT", 3: "QUIT", 15: "TERM"} | |
print("Detected {}:SIG{}!".format(signum, signames[signum])) | |
except Exception as e: | |
raise | |
finally: | |
sys.exit(0) | |
if __name__ == '__main__': | |
try: | |
print("PID =", os.getpid()) | |
obj = TestSignal() | |
obj.exec() | |
except Exception as e: | |
traceback.print_exc() | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment