当父线程退出时,Python守护线程不会退出(Python daemon thread does not exit when parent thread exits)

编程入门 行业动态 更新时间:2024-10-25 22:29:13
当父线程退出时,Python守护线程不会退出(Python daemon thread does not exit when parent thread exits)

我有一些创建恶魔线程的Python代码。 父线程几乎立即结束,但守护线程继续打印睡眠。

import threading import time def int_sleep(): for _ in range(1, 600): time.sleep(1) print("sleep") def main(): thread = threading.Thread(target=int_sleep) thread.daemon = True thread.start() time.sleep(2) print("main thread end...") thread = threading.Thread(target=main) thread.start()

内容sys.version:

'3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:19:30) [MSC v.1600 64 bit (AMD64)]'

打印:

sleep main thread end... sleep sleep sleep

为什么当父线程退出时,Python守护进程线程不会退出?

I have some Python code that creates a demon thread. The parent thread ends almost immediately, but the daemon thread keeps printing sleep.

import threading import time def int_sleep(): for _ in range(1, 600): time.sleep(1) print("sleep") def main(): thread = threading.Thread(target=int_sleep) thread.daemon = True thread.start() time.sleep(2) print("main thread end...") thread = threading.Thread(target=main) thread.start()

sys.version:

'3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:19:30) [MSC v.1600 64 bit (AMD64)]'

Prints:

sleep main thread end... sleep sleep sleep

Why doesn't the Python daemon thread exit when parent thread exits?

最满意答案

如果你为python线程指定了thread.daemon = True ,那么当只有守护进程被留下时,程序会立即停止。 发送到stdout的命令会丢失。

将其添加到名为main.py的文件中

import threading import time def int_sleep(): for _ in range(1, 600): time.sleep(1) print("sleep") def main(): thread = threading.Thread(target=int_sleep) thread.daemon = True thread.start() time.sleep(2) print("main thread end...") thread = threading.Thread(target=main) thread.daemon = True thread.start()

像这样运行它:

el@apollo:~/code/python/run01$ python --version Python 2.7.6 el@apollo:~$ python main.py el@apollo:~$

看到它没有打印任何东西,因为线程开始 您将其设置为守护进程并启动它。 然后程序结束。

附加说明:如果将此代码粘贴到python解释器中,则所有打印语句都将显示在终端上,因为守护进程永远不会丢失与stdout的连接。

阅读更多: http : //docs.python.org/2/library/threading.html

If you specify thread.daemon = True for your python thread, then the program will halt immediately when only the daemon is left. The the commands sent to stdout are lost.

Add this to a file called main.py

import threading import time def int_sleep(): for _ in range(1, 600): time.sleep(1) print("sleep") def main(): thread = threading.Thread(target=int_sleep) thread.daemon = True thread.start() time.sleep(2) print("main thread end...") thread = threading.Thread(target=main) thread.daemon = True thread.start()

Run it like this:

el@apollo:~/code/python/run01$ python --version Python 2.7.6 el@apollo:~$ python main.py el@apollo:~$

See it prints nothing because the thread started. You set it to be a daemon and started it. Then the program ended.

Extra notes: If you paste this code into a python interpreter, all the print statements will appear on the terminal because the daemon never loses hold of its connection to stdout.

Read more: http://docs.python.org/2/library/threading.html

更多推荐

本文发布于:2023-08-01 04:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353824.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   daemon   Python   thread   parent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!