I am trying to understand daemon threads in Python. My understanding is that daemon threads are automatically killed once the main thread exits or non-daemonic threads are killed. However, on a Windows machine, that is not my observation.
import threading
import time
def print_work_a():
print('Starting of thread :', threading.currentThread().name)
time.sleep(2)
print('Finishing of thread :', threading.currentThread().name)
def print_work_b():
print('Starting of thread :', threading.currentThread().name)
print('Finishing of thread :', threading.currentThread().name)
a = threading.Thread(target=print_work_a, name='Thread-a', daemon=True)
b = threading.Thread(target=print_work_b, name='Thread-b')
a.start()
b.start()
Output observed:
>>> Starting of thread :Thread-a
Starting of thread :Thread-b
Finishing of thread :Thread-b
Finishing of thread :Thread-a
I was expecting that the output wouldn't contain Finishing of thread :Thread-a since by that time the non-daemon thread would be killed and hence the daemon thread would also be killed. What is the mistake in the code that is causing the daemon thread to remain alive?