English 中文(简体)
为什么在使用沙尔的翻新模块时,白喉旗有用?
原标题:Why is the dameon flag useful when using the threading module in Python?

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()


问题回答

重复增加清晰度。 我认识到,当一个方案即将退出时,马丹马透镜显示其真实的肤色。 思维方式,如 de魔,或廉价、无心的粉饰”,, 这里提及的是, 谷歌,除非已经完工或未完成,否则将突然停止种植。 因此,Daemon 如下文所述,“完成胎盘”和(或)等待/锁定(见.join(<>>>>/code>,Non-Daemons.,<>BUT,caveats <>/em>(或在此情况下,造成线形状的执行)。

Daemon = 非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() 

<>>>>

  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()

我的混淆导致我的原职是2个部分。 我认为,终止方案(因此,包括sys.exit)与“自成一体”方案(案文是关键,但 da诉非mon,没有区别)之间可能有所不同,还有以下事实:有“主导”执行,你can>/em>等。 希望这能帮助人们理解沙捞越了一点!

Edit: 改进解释





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...