Skip to content

Instantly share code, notes, and snippets.

@ipedrazas
Created August 22, 2020 04:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipedrazas/441d8645c9e53d2a6473d45be89f5eb6 to your computer and use it in GitHub Desktop.
Save ipedrazas/441d8645c9e53d2a6473d45be89f5eb6 to your computer and use it in GitHub Desktop.
Processing SIGTERM in python
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