Created
July 9, 2019 07:31
-
-
Save eshleebien/98fc2b90ff4bcd7e3e6ae5ce14f55219 to your computer and use it in GitHub Desktop.
For my medium article "Containers: Terminating with grace"
This file contains hidden or 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
# source code | |
# shamelessly copied from | |
# https://stackoverflow.com/a/31464349/2591014 | |
import signal | |
import time | |
class GracefulKiller: | |
kill_now = False | |
signals = { | |
signal.SIGINT: 'SIGINT', | |
signal.SIGTERM: 'SIGTERM' | |
} | |
def __init__(self): | |
signal.signal(signal.SIGINT, self.exit_gracefully) | |
signal.signal(signal.SIGTERM, self.exit_gracefully) | |
def exit_gracefully(self, signum, frame): | |
print("\nReceived {} signal".format(self.signals[signum])) | |
print("Cleaning up resources. End of the program") | |
self.kill_now = True | |
if __name__ == '__main__': | |
killer = GracefulKiller() | |
print("Running ...") | |
while not killer.kill_now: | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment