from threading import Thread
from time import sleep
def worker(i):
while True:
print("worker {}".format(i))
sleep(i)
threads = [Thread(target=worker, args=(i,)) for i in range(1, 4)]
for t in threads:
t.start()
for t in threads:
t.join()
A thread can be flagged as a “daemon thread”. The significance of this flag is that the entire Python program exits when only daemon threads are left. The initial value is inherited from the creating thread. The flag can be set through the daemon property or the daemon constructor argument.