gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:50 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com