Skip to content

Instantly share code, notes, and snippets.

@funsim
Created April 16, 2018 05:56
Show Gist options
  • Save funsim/56b362bad29cfbcc3879717883d3b191 to your computer and use it in GitHub Desktop.
Save funsim/56b362bad29cfbcc3879717883d3b191 to your computer and use it in GitHub Desktop.
import threading
import signal
import sys
from time import sleep
class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.is_running = False
def run(self):
self.is_running = True
while self.is_running:
print("Thread is running")
sleep(0.1)
print("Thread exiting")
def stop(self):
self.is_running = False
thread = MyThread()
thread.start()
def sigterm_handler(_signo, _stack_frame):
thread.stop()
sys.exit(0)
signal.signal(signal.SIGTERM, sigterm_handler)
signal.signal(signal.SIGINT, sigterm_handler)
signal.signal(signal.SIGHUP, sigterm_handler)
while True:
sleep(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment