守护程序线程与守护程序进程 [英] Daemon threads vs daemon processes

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

问题描述

基于 Python文档,守护进程线程是主线程死亡后死亡的线程.这似乎是守护进程完全相反的行为,它涉及创建子进程并终止父进程以使init接管子进程(也就是杀死父进程不会杀死子进程).

Based on the Python documentation, daemon threads are threads that die once the main thread dies. This seems to be the complete opposite behavior of daemon processes which involve creating a child process and terminating the parent process in order to have init take over the child process (aka killing the parent process does NOT kill the child process).

那么,为什么父线程死时守护程序线程死了,这是错误的称呼吗?我认为"daemon"线程将在主进程终止后继续运行.

So why do daemon threads die when the parent dies, is this a misnomer? I would think that "daemon" threads would keep running after the main process has been terminated.

推荐答案

它只是名称,表示在不同上下文中的不同事物.

It's just names meaning different things in different contexts.

如果您不知道,例如threading.Threadmultiprocessing.Process也可以标记为守护程序".您对守护进程"的描述适合Unix守护进程,而不适合Python的守护进程.

In case you are not aware, like threading.Thread, multiprocessing.Process also can be flagged as "daemon". Your description of "daemon processes" fits to Unix-daemons, not to Python's daemon-processes.

文档中还有关于 Process.daemon :

The docs also have a section about Process.daemon:

...请注意,不允许守护进程创建子进程. 否则,守护进程将使其子进程变成孤儿 当其父进程退出时终止.另外,这些是 不是Unix守护程序或服务,它们是正常的进程,将 如果非守护进程已退出,则终止(并且不加入).

... Note that a daemonic process is not allowed to create child processes. Otherwise a daemonic process would leave its children orphaned if it gets terminated when its parent process exits. Additionally, these are not Unix daemons or services, they are normal processes that will be terminated (and not joined) if non-daemonic processes have exited.

Python的守护进程和Unix守护进程(或Windows服务")之间唯一的共同之处是您可以使用将它们用于后台任务 (对于Python:不过,对于关闭时不需要适当清理的任务来说,这只是一个选择).

The only thing in common between Python's daemon-processes and Unix-daemons (or Windows "Services") is that you would use them for background-tasks (for Python: only an option for tasks which don't need proper clean up on shutdown, though).

Python在OS线程和进程之上强加了自己的抽象层. ThreadProcess的守护程序属性是关于与操作系统无关,Python级别的抽象的.

Python imposes it's own abstraction layer on top of OS-threads and processes. The daemon-attribute for Thread and Process is about this OS-independent, Python-level abstraction.

在Python级别,守护进程线程是在主线程退出时不被加入(等待自愿退出)的线程,而守护进程是在以下情况被终止(未加入)的线程:父进程退出.守护程序线程和进程都经历相同的行为,因为如果主进程或父进程关闭,则不会等待其自然退出.就是这样.

At the Python-level, a daemon-thread is a thread which doesn't get joined (awaited to exit voluntarily) when the main-thread exits and a daemon-process is a process which gets terminated (not joined) when the parent-process exits. Daemon-threads and processes both experience the same behavior in that their natural exit is not awaited in case the main or parent-process is shutting down. That's all.

请注意,Windows甚至没有像Unix这样的相关进程"的概念,但是Python以跨平台的方式实现了子"和父"的这种关系.

Note that Windows doesn't even have the concept of "related processes" like Unix, but Python implements this relationship of "child" and "parent" in a cross-platform manner.

我认为守护程序"线程将在主线程之后继续运行 进程已终止.

I would think that "daemon" threads would keep running after the main process has been terminated.

线程不能存在于进程的外部.进程始终托管并为至少 个线程提供上下文.

A thread cannot exist outside of a process. A process always hosts and gives context to at least one thread.

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

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