- 主题:daemon设置为True,子线程也不随主线程结束
开始学习python中的多线程,可是我发现按照书上的例子,将子线程的daemon无论设成True还是False,结果都一样,子线程都不能随主线程的结束而结束,而且每次运行,结果都不完全一样,不知道是怎么回事?
程序如下:
import threading
import time
def reading():
for i in range(3):
print('reading',i)
time.sleep(1)
r=threading.Thread(target=reading)
r.setDaemon(True)
r.start()
print('The End')
--
FROM 1.95.148.*
我觉得您没有理解我的意思。
我当然知道打印一个“the end”并不是就结束了,实际上,最后一句print("the end")无关紧要。
我的意思是:按照书上的说法,setDaemon为True时,子线程应该与主线程同时结束,但我实际运行的结果不是这样,无论setDameno设置为True还是False,子线程都不与主线程同时结束,还是在主线程结束后继续运行子线程。
【 在 ToSimplicity 的大作中提到: 】
: 不是你打印一个“The End”,它就结束了。。。
:
https://docs.python.org/3/library/threading.html#thread-objects: 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 or the daemon constructor argument.
: ...................
--
FROM 1.95.148.*
谢谢指教。
但我随便在网上搜setDaemon,都是这么说的。而且我发现网上的举例我实际运行出来根本不对。
比如下面这个链接:
https://blog.csdn.net/zhangzheng0413/article/details/41728869?utm_medium=distribute.pc_relevant_download.none-task-blog-2~default~BlogCommendFromBaidu~default-2.test_version_3&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-2~default~BlogCommendFromBaidu~default-2.test_version_
这里面2是讲的setDaemon,我把里面2.X的语法改成3.X的语法然后运行,和文中的结果根本不一样。
我现在都怀疑是不是2.x和3.x在这方面完全不同。
【 在 ToSimplicity 的大作中提到: 】
: 哪本书这么说的?
: In multitasking computer operating systems, a daemon (/'di:m?n/ or /'de?m?n/)[1] is a computer program that runs as a background process, rather than being under the direct control of an interactive user.
: doc的说明是精准的,主程序想要退出时根本不理daemon线程,说走就走。
: ...................
--
FROM 1.95.148.*
非常感谢,我到不是非要学python的多线程。说实话,我只是业余学学python,根本用不到线程呢。
只是跟着“中国大学Mooc”的一个视频在一点点学,学到了这儿,怎么也过不去,所以到水木来请教高手了,看看我到底是哪儿错了?
【 在 hgoldfish 的大作中提到: 】
: 扔了 python 的多线程吧。试试 asyncio 或者 greenlet 协程。
: 协程的好处是它的中断都是可控的,我写两个版本给你看看:
: gevent 版本:
: ...................
--
FROM 1.95.148.*
非常非常感谢。
当语句为setDaemon(False)时,我测试了五次,结果如下:
第一次:
The Endreading
0
>>>
reading 1
reading 2
第二次:
readingThe End
0
>>>
reading 1
reading 2
第三次:
readingThe End
0
>>>
reading 1
reading 2
第四次:
readingThe End
0
>>>
reading 1
reading 2
第五次:
The Endreading
0
>>>
reading 1
reading 2
当语句为setDaemon(True)时,我又测试了五次,结果如下:
第一次:
readingThe End
0
>>> reading 1
reading 2
第二次:
readingThe End
0
>>>
reading 1
reading 2
第三次:
The Endreading
>>> 0
reading 1
reading 2
第四次:
readingThe End
0
>>>
reading 1
reading 2
第五次:
The Endreading
0
>>>
reading 1
reading 2
感觉无论是True还是False,结果没有什么区别,而且结果是随机的。
【 在 wincss 的大作中提到: 】
: 我觉得你的问题可能是这样
: 并不是说打印了 The End 主线程就会“立刻”结束,可能还会持续一小段时间,所以
: 可能足够 daemon 的子线程打出来点东西
: ...................
--
FROM 1.95.148.*
多谢提醒。
当语句为setDaemon(False)时:
我期望的输出是(这是书中给出的输出结果):
reading 0
The End
reading 1
reading 2
实际的输出中,上面四句倒是都能打印出来,但顺序有问题,有点随机(详见上一楼我的回复)。
当语句为setDaemon(True)时:
我期望的输出是(书中给出的输出结果):
reading 0
The End
但在实际的输出中,reading 1和reading 2还是都能打印出来(详见上一楼我的回复)。
所以我就很疑惑,为什么在我这儿,setDaemon设成True或False没区别呢。
【 在 ilovecpp 的大作中提到: 】
: 建议你按照提问/报bug的规范来。
: 你期望的输出是什么:
: 实际的输出是什么:
--
FROM 1.95.148.*
我只会用windows,从来没用过linux

不过还是非常非常感谢。
【 在 DigitalBeing 的大作中提到: 】
: 试试在linux terminal里运行这个文件,不要在IDE或者windows command line里运行
:
--
FROM 1.95.148.*
多谢多谢。
不过我的运行结果和您的不同,而且True时,fun也还是在main之后全部运行完。
我用了两个环境测试(虽然结果中顺序不完全一致,但都是true时fun不能随main结束):
一个是python 3.7.3(32-bit),在IDLE中运行。第二个是spyder 5.0.3,里面的python好像是3.8.8 64-bit
【 在 jlsthsdqyx 的大作中提到: 】
: import threading
: import time
: def fun():
: ...................
--
FROM 1.95.148.*
我用了两个环境测试(都是windows,因为我不会用linux,只会用windows):
一个是python 3.7.3(32-bit),在IDLE中运行。第二个是spyder 5.0.3,里面的python好像是3.8.8 64-bit
我不是在IDLE中一行行敲的代码,是写了一个.py的文件,然后运行得到的结果。
>>>是显示运行结果的IDLE中自带的,说明后面跟的是运行结果,全景是下面这个样子。
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
=== RESTART: D:\example3-3-1.py ===
The Endreading
>>> 0
reading 1
reading 2
【 在 ilovecpp 的大作中提到: 】
: 我试了下,输出符合你的期望。Python 3.9.5, Linux。
: 理论上
: The End
: ...................
--
FROM 1.95.148.*
多谢多谢,看了大家的回复后,我也逐渐感觉是我的运行环境有问题,准备换个pycharm试试。
【 在 galaxy123 的大作中提到: 】
:
: 我用 Python 3.7.3 在 Mac 平台下的 PyCharm IDE 中测试,结果完全符合预期. 可能你的Python环境有问题。
: 代码:
: ...................
--
FROM 1.95.148.*