Last active
June 3, 2022 04:56
-
-
Save khaerulumam42/bf7ae73066cf85d326e7e345e6398afa to your computer and use it in GitHub Desktop.
gracfully kill 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
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