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的答案,我不清楚在主线程调用
参考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
简而言之,是的,守护进程线程不会阻止程序退出,因此它们将在退出时被杀死。