Skip to content

Instantly share code, notes, and snippets.

@liviaerxin
Created May 20, 2022 01:55
Show Gist options
  • Save liviaerxin/6f9ac1fa86a4a98ba8c7b9d430c00a57 to your computer and use it in GitHub Desktop.
Save liviaerxin/6f9ac1fa86a4a98ba8c7b9d430c00a57 to your computer and use it in GitHub Desktop.
Dummy thread to close gracefully
import threading
import time
import threading
import signal
class DummyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self._running = True
signal.signal(signal.SIGINT, self.stop)
signal.signal(signal.SIGTERM, self.stop)
def stop(self, *args):
print(f"print stop {args}")
self._running = False
def run(self):
while self._running:
time.sleep(1)
print("Running")
if __name__ == "__main__":
t = DummyThread()
t.daemon = True
t.start()
t.join()
print("Exit")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment