Python 线程上守护进程属性的含义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4330111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:17:43  来源:igfitidea点击:

Meaning of daemon property on Python Threads

pythonmultithreadingdaemonpython-multithreading

提问by Falmarri

I'm a little confused about what setting a thread to be a daemon means.

我对将线程设置为守护进程意味着什么感到有些困惑。

The documentation says this:

文档是这样说的:

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. The flag can be set through the daemon property.

一个线程可以被标记为“守护线程”。这个标志的意义在于,当只剩下守护线程时,整个 Python 程序就会退出。初始值是从创建线程继承的。该标志可以通过 daemon 属性设置。

I'm not sure what makes this different from a normal thread.

我不确定是什么使这与普通线程不同。

Is this saying that this program won't ever finish?

这是说这个程序永远不会完成吗?

def threadfunc():
    while True:
        time.sleep(1)

threading.Thread(target=threadfunc).start()

Even though the main thread finishes it's execution. While will finish immediately?

即使主线程完成它的执行。虽然会立即完成?

def threadfunc():
    while True:
        time.sleep(1)

th = threading.Thread(target=threadfunc)
th.daemon = True
th.start()

I ask because I have a situation where in my main thread I'm calling sys.exit(), and the process just hangs and my other threads are running as I can see the log.

我问是因为我在我的主线程中调用 sys.exit() 的情况下,该进程挂起而我的其他线程正在运行,因为我可以看到日志。

Does this have anything to do with sys.exit() being called with threads alive?

这与 sys.exit() 被调用时线程活着有什么关系吗?

采纳答案by Jochen Ritzel

Is this saying that this program won't ever finish?

这是说这个程序永远不会完成吗?

Yes, that program won't finish, just try it out.

是的,该程序不会完成,请尝试一下。

I ask because I have a situation where in my main thread I'm calling sys.exit(), and the process just hangs and my other threads are running as I can see the log. Does this have anything to do with sys.exit() being called with threads alive?

我问是因为我在我的主线程中调用 sys.exit() 的情况下,该进程挂起而我的其他线程正在运行,因为我可以看到日志。这与 sys.exit() 被调用时线程活着有什么关系吗?

Yes, even exitwon't stop other threads, it simply raises SystemExitin the main thread. So while the main thread will stop (just like it does on any other unhandled Exception), all other non-daemonic threads will continue to work.

是的,甚至exit不会停止其他线程,它只是SystemExit在主线程中引发。因此,虽然主线程将停止(就像它在任何其他未处理的异常上一样),但所有其他非守护线程将继续工作。

回答by TelegramSam

Setting thread.daemon = Truewill allow the main program to exit. Apps normally wait till all child threads are finished before completing.

设置thread.daemon = True将允许主程序退出。应用程序通常会等到所有子线程都完成后再完成。

回答by Alejandro Serret

th.daemon = True #set this thread as a Daemon Thread

You can think in a Daemon thread as a service this means that it will be running in the background of your computer doing differents task, like indexing files, parsing xml, retrieving news etc, anything that is a long running process.

您可以将守护进程线程视为服务,这意味着它将在您的计算机后台运行,执行不同的任务,例如索引文件、解析 xml、检索新闻等,任何需要长时间运行的进程。

Your Main thread will finish and your daemon will still be running in the background, that is the reason why your program aka Main thread finish, if you want just put an infinite loop and you will see your thread still running. An example for a daemon thread is the garbage collection.

您的主线程将完成并且您的守护程序仍将在后台运行,这就是您的程序又名主线程完成的原因,如果您只想放置一个无限循环并且您将看到您的线程仍在运行。守护线程的一个例子是垃圾收集。