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

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

问题描述

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?

推荐答案

某些线程执行后台任务,例如发送保持活动的数据包,或执行定期垃圾收集,或其他任何事情.这些仅在主程序运行时有用,一旦其他非守护进程退出,就可以将它们杀死.

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天全站免登陆