守护进程线程与Python中的守护进程 [英] Daemon threads vs daemon processes in Python

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

问题描述

基于 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.Thread multiprocessing.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线程和进程。 Thread Process 的守护程序属性是关于这种与操作系统无关的,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在Windows中实现了子和父的关系

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.

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

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