#setting thread as a daemon thread
import threading
import time
import sys
def exfu():
while True:
time.sleep(0.5)
print('Thread alive, but it will die on program termination')
x = threading.Thread(target=exfu)
x.daemon = True
x.start()
time.sleep(2)
sys.exit()
#using _stop()
import time
import threading
class th1(threading.Thread):
def __init__(self, *args, **kwargs):
super(th1, self).__init__(*args, **kwargs)
self._stop = threading.Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
while True:
if self.stopped():
return
print("Hello, world!")
time.sleep(1)
x = th1()
x.start()
time.sleep(5)
x.stop()
x.join()