Created
May 20, 2022 01:55
-
-
Save liviaerxin/6f9ac1fa86a4a98ba8c7b9d430c00a57 to your computer and use it in GitHub Desktop.
Dummy thread to close gracefully
This file contains hidden or 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
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