Skip to content

Instantly share code, notes, and snippets.

@komasaru komasaru/test_signal.py
Last active Feb 23, 2018

Embed
What would you like to do?
Python script to test interrupt handling by signal.
#! /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
You can’t perform that action at this time.