Python初学者会有一个误解,那就是设置线程为Daemon,主线程退出后,子线程仍运行直到任务结束。其实,这是不对的。
维基百科是这样定义守护程序的
In multitasking computer operating systems, a daemon (/ˈdiːmən/ or /ˈdeɪmən/)is a computer program that runs as a background process, rather than being under the direct control of an interactive user.
可见,这里并没有说到守护线程是否会跟随主线程一起退出,仅仅提到了在后台运行,并且不和用户直接交互。
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.
要点:线程可以通过setDaemon(True)被设置为守护线程,当仅有守护线程运行时,主程序才能退出。
Daemons are only useful when the main program is running, and it’s okay to kill them off once the other non-daemon threads have exited. Without daemon threads, we have to keep track of them, and tell them to exit, before our program can completely quit. By setting them as daemon threads, we can let them run and forget about them, and when our program quits, any daemon threads are killed automatically.
主线程启动两个子线程:代码地址:threading_daemon.py
根据我们上面的总结,我们会知道:
import time
import threading
def sub(num):
if i % 2 == 0:
time.sleep(10)
else:
time.sleep(1)
print("thread-{0}: done\n".format(num))
if __name__ == "__main__":
for i in range(2):
t = threading.Thread(target=sub, args=(i, ))
if i % 2 == 0:
t.setDaemon(True)
t.start()
print("thread-{0} started".format(i))
运行结果如下:
thread-0 started
thread-1 started
thread-1: done