2

Why doesn't the main thread kill the daemon one?

import threading
import time

print_lock = threading.Lock()

def exampleNoneDaemon():
    for x in range(3):
        with print_lock:
            print(x, "master")
        time.sleep(0.7)

t1 = threading.Thread(target=exampleNoneDaemon)
t1.start()

def exampleDaemon():
    while True:
        with print_lock:
            print("daemon")
        time.sleep(1)

t2 = threading.Thread(target=exampleDaemon)
t2.daemon = True
t2.start()

the output of this code is:

0 master
daemon
1 master
daemon
2 master
daemon
daemon
daemon
daemon
...

What have I done wrong?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Omer B
  • 53
  • 4

0 Answers0