为什么在 Python 中使用线程模块时 daeon 标志很有用?

发布于 2025-01-10 19:10:02 字数 520 浏览 0 评论 0原文

给定如下简单脚本,为什么使用守护进程标志有用?也许这太简单了,但我知道守护线程通常是长时间运行的后台任务(正如 Raymond Hettinger 引用的那样,它们并不意味着要等待)。那么,如果我有一个我不等待的任务,并且只是启动一个非守护进程线程而不加入,那么这是伪守护进程吗?看起来功能是一样的。或者这更多的是记忆问题而不是处理逻辑问题?对于第二个问题,我实际上不确定这个脚本在守护进程与非守护进程方面消耗了多少资源


from threading import Thread 
import time
import sys
def func():
    for i in range(4):
        print(f"Running Thread-{i}")
        time.sleep(1)


t = Thread(target=func)
# t.daemon = True # nothing seems to change
t.start()
sys.exit()


Given a simple script as the following, why is using daemon flag useful? Maybe this is too simple, but I understand that daemon threads are generally long running background tasks (they are not meant to be waited for as quoted by Raymond Hettinger). So, if I have a task that I am not waiting for and simply start a non-daemon thread and not join, is that psuedo-daemon? It seems the functionality runs the same. Or is this more of a question of memory than processing logic? With the second question Im actually not sure how much resources this script consumes in the aspects of daemon vs non-daemon


from threading import Thread 
import time
import sys
def func():
    for i in range(4):
        print(f"Running Thread-{i}")
        time.sleep(1)


t = Thread(target=func)
# t.daemon = True # nothing seems to change
t.start()
sys.exit()


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月染过的梦 2025-01-17 19:10:02

回来增加清晰度。我意识到当程序退出时,守护线程会显示出它们的真面目。把恶魔想象成恶魔,或者可鄙的、无意识的小卒子。 正如此处提到的,Python 会突然停止守护进程,无论它们是否已完成。因此,到目前为止,守护进程线程应该用于完成无关紧要的任务,以及等待/阻塞(即用于非守护进程的.join()但是存在警告(或者在本例中导致线条模糊的实现),如下所述

守护进程==非守护进程 条件。

  1. 。我们在这里等待 所有线程虽然这“有效”,但守护线程应该只是一个非守护线程,在这种情况下,就是向代码读取器/维护者发出指令。这些线程都需要等待,这没有多大意义(雷蒙德所说的确实有道理,“不要等待守护进程”)
# Psuedo-code

d_t = DaemonThread

n_t = NonDaemonThread

for thread_ in [d_t, n_t]:
  thread_.start() 

for thread_ in [d_t, n_t]:
  thread_.join() 

OR

  1. 这里我们不等待任何这不仅意味着我们不需要等待任务,而且现在非守护线程将在程序退出时突然停止,因为
# Psuedo-code

d_t = DaemonThread

n_t = NonDaemonThread

for thread_ in [d_t, n_t]:
  thread_.start() 

# never call join()

我最初的帖子的混乱是由两部分组成的。我认为终止的程序(因此简单地包括 sys.exit )与“自行退出”的程序(上下文是关键,但守护程序与非守护程序的情况)之间可能存在差异,没有区别),而且事实是,通过“误导性”实现,您可以等待守护进程线程或突然杀死非守护进程。希望这可以帮助人们更多地了解 Python!

编辑:改进的解释

Coming back to add clarity. I realized that daemon threads show their true colors when a program is exiting. Think of daemons like demons, or despensable, mindless, little pawns. As mentioned here, Python will abruptly stop daemons, regardless if they have completed or not. So to this point, Daemon threads should be used for tasks where completion doesnt matter, and waiting/blocking (ie .join() for Non-Daemons. BUT there caveats (or in this case implementations that causes blurred lines) as described below.

Daemon == Non-daemon on 2 conditions.

  1. Here we are waiting for ALL THREADS. Although this "works", the daemon thread should just be a non-daemon. Flagging a thread as daemon, in this case, is giving instructions (to the code reader/maintiner as well) that these threads all need to be waited for, which doesnt make much sense (and what Raymond said does make sense, "Dont wait for daemons")
# Psuedo-code

d_t = DaemonThread

n_t = NonDaemonThread

for thread_ in [d_t, n_t]:
  thread_.start() 

for thread_ in [d_t, n_t]:
  thread_.join() 

OR

  1. Here we aren't waiting for ANY threads. Not only does this imply that we dont need to wait for tasks BUT now the non-daemon threads, will be abruptly stopped when the program exits, since they are not waited for.
# Psuedo-code

d_t = DaemonThread

n_t = NonDaemonThread

for thread_ in [d_t, n_t]:
  thread_.start() 

# never call join()

My confusion leading to my original post was 2 parts. I was thinking there might be a difference between a terminated program ( hence trivially including sys.exit) vs one that "exits on its own" (context is key but the case of daemon vs non-daemon, no difference) and also that fact that yes, with "misleading" implementation, you can wait for daemon threads or abrupty kill non-daemons. Hopefully this helps someone understand Python a little more!

Edit: Improved explanation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文