Skip to content

Instantly share code, notes, and snippets.

@khaerulumam42
Last active June 3, 2022 04:56
Show Gist options
  • Save khaerulumam42/bf7ae73066cf85d326e7e345e6398afa to your computer and use it in GitHub Desktop.
Save khaerulumam42/bf7ae73066cf85d326e7e345e6398afa to your computer and use it in GitHub Desktop.
gracfully kill python
from datetime import datetime
import time
import signal
import logging
class GracefulKiller:
def __init__(self):
self.kill_now = False
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self, signum, frame):
logging.warning('gracefully exitting')
self.kill_now = True
def start():
g = GracefulKiller()
logging.warning(f'start state : {g.kill_now}')
i = 0
while not g.kill_now:
logging.warning(i)
logging.warning(f'start : {datetime.now()}')
time.sleep(3)
logging.warning(f'end : {datetime.now()}')
i += 1
logging.warning(f'state now : {g.kill_now}')
if __name__ == '__main__':
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment