python - 当主线程调用 sys.exit() 时,守护线程是否会被终止?

标签 python python-3.x multithreading

根据文档: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.

示例代码:

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

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

关于python - 当主线程调用 sys.exit() 时,守护线程是否会被终止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54475760/

相关文章:

python - 在 HTML 电子邮件中使用变量?

python - time.sleep 阻止按下 tkinter.Button

java - 如何在 Java 中实现多线程 MergeSort

python - 如何获取查询集中外键字段的字段

python - 移植 C 定义了 Pythonic 方式

python - 双链表中间插入不起作用

string - str.find() 遇到问题

python - 如何验证 namedtuple 值?

multithreading - 在指定数量的内核上运行MPI程序

java - 在 AWT 线程中运行代码