Created
August 22, 2020 04:47
-
-
Save ipedrazas/441d8645c9e53d2a6473d45be89f5eb6 to your computer and use it in GitHub Desktop.
Processing SIGTERM in python
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
import signal | |
import time | |
class GracefulKiller: | |
kill_now = False | |
def __init__(self): | |
signal.signal(signal.SIGINT, self.exit_gracefully) | |
signal.signal(signal.SIGTERM, self.exit_gracefully) | |
def exit_gracefully(self,signum, frame): | |
self.kill_now = True | |
if __name__ == '__main__': | |
killer = GracefulKiller() | |
while not killer.kill_now: | |
time.sleep(1) | |
print("doing something in a loop ...") | |
print("End of the program. I was killed gracefully :)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment