python - Why doesn't daemon thread die when its environment is destroyed? -
i have python script starts thread thread. learning exercise realise ability kill python thread, have use in application writing. ignoring red herring, thread firstthread
starts thread secondthread
our purposes caught in loop , has no resources release. consider:
import threading import time class firstthread (threading.thread): def run(self): b = secondthread() b.daemon = true b.start() time.sleep(3) print("firstthread going away!") return true class secondthread (threading.thread): def run(self): while true: time.sleep(1) print("secondthread") = firstthread() a.daemon = true a.start() print("waiting 5 seconds.") time.sleep(5) print("done waiting")
although firstthread
print "firstthread going away!" after 3 seconds expected, secondthread
continues print "secondthread" stdout. expected secondthread
destroyed firstthread
destroyed a daemon thread. why secondthread
continue exist though environment (firstthread) has been destroyed?
threads not hierarchical in nature. afaik, thread maintains closure on environment created in it. threads owned process. daemon
thread thread not joined before application exits. in short, there no tie between firstthread
, secondthread
in example.
Comments
Post a Comment