关于python:主线程调用sys.exit()时是否会杀死守护程序线程?

Are daemon threads killed when main thread calls sys.exit()?

根据文档:https://docs.python.org/3/library/threading.html

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.

样例代码:

1
2
thread = threading.Thread(target=MultiHandler().network, args=(conn, data), daemon=True)
thread.start()

参考许多其他stackoverflow的答案,我不清楚在主线程调用sys.exit()时是否强制关闭守护进程线程。


参考Zwer发表的评论,

When a program exits, all of its children threads are killed with it. Threads that are not daemonic will prevent the program from exiting, hence preventing their own destruction. - zwer

简而言之,是的,守护进程线程不会阻止程序退出,因此它们将在退出时被杀死。