Skip to content

Instantly share code, notes, and snippets.

@mydreambei-ai
Created July 18, 2016 07:43
Show Gist options
  • Save mydreambei-ai/62f22474b1c733cfd0c94089f43ce6cc to your computer and use it in GitHub Desktop.
Save mydreambei-ai/62f22474b1c733cfd0c94089f43ce6cc to your computer and use it in GitHub Desktop.
stop thread in python
import select
import threading
import time
class StoppableThread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__(self):
super(StoppableThread, self).__init__()
self._stop = threading.Event()
def run(self):
while not self.stopped():
print("name:{}".format(self.name))
time.sleep(0.8)
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
if __name__ == '__main__':
t1 = StoppableThread()
t2 = StoppableThread()
try:
t1.daemon = True
t1.start()
while 1:
select.select([], [], [])
except (KeyboardInterrupt, SystemExit):
t1.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment