Skip to content

Instantly share code, notes, and snippets.

@simryang
Last active July 10, 2020 08:19
Show Gist options
  • Save simryang/cfbddc0b010b05e1491fee7cc7e2f146 to your computer and use it in GitHub Desktop.
Save simryang/cfbddc0b010b05e1491fee7cc7e2f146 to your computer and use it in GitHub Desktop.
import time
import threading
class testthread(threading.Thread):
def __init__(self, testvar=None):
threading.Thread.__init__(self)
self.enabled = False
def stop(self):
self.enabled = False
def enable(self):
self.enabled = True
def run(self):
try:
while self.enabled:
print('1')
time.sleep(0.1)
except Exception as e:
pass
if __name__ == '__main__':
ttest = testthread('abcd')
ttest.enable()
ttest.start()
time.sleep(0.001)
ttest.stop()
@simryang
Copy link
Author

Set enabled flag by calling ttest.enable()
Unset enabled flag by calling ttest.stop()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment