守护程序线程说明 [英] Daemon Threads Explanation

查看:84
本文介绍了守护程序线程说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python文档中 它说:

可以将线程标记为守护程序线程".的意义 标志是只有守护程序线程时,整个Python程序都会退出 离开了.初始值是从创建线程继承的.

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.

有人对这意味着什么有更清楚的解释吗,或者有一个实际的示例显示了将线程设置为daemonic的位置?

Does anyone have a clearer explanation of what that means or a practical example showing where you would set threads as daemonic?

为我澄清一下:因此,您唯一不希望将线程设置为daemonic的情况是,您希望它们在主线程退出后继续运行吗?

Clarify it for me: so the only situation you wouldn't set threads as daemonic, is when you want them to continue running after the main thread exits?

推荐答案

某些线程执行后台任务,例如发送keepalive数据包或执行定期垃圾回收等.这些仅在主程序正在运行时才有用,并且可以在其他非守护程序线程退出后将其杀死.

Some threads do background tasks, like sending keepalive packets, or performing periodic garbage collection, or whatever. These 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, you'd have to keep track of them, and tell them to exit, before your program can completely quit. By setting them as daemon threads, you can let them run and forget about them, and when your program quits, any daemon threads are killed automatically.

这篇关于守护程序线程说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆