Does Daemon Thread Exit with Main Thread?
主线程(进程)退出后,主线程创建的守护线程也会退出吗?
通过下面的代码测试:
Demo1: 进程创建普通线程
#!/usr/bin/python3
# FileName: daemonThread.py
# Author: lxw
# Date: 2016-02-25 import threading
import time def show(num):
time.sleep(3)
print("In show(): {0}".format(num))
with open("/home/lxw/Documents/NOTE", "w") as f:
f.write("hello") def main():
th = threading.Thread(target=show, args=(1,))
#th.setDaemon(True)
th.start()
print("main() over. Program exit!") if __name__ == '__main__':
main()
else:
print("Being imported as a module.")
运行结果:
lxw Documents$ python3 daemonThread.py
main() over. Program exit!
In show():
lxw Documents$
"The entire Python program exits when no alive non-daemon threads are left".
只有当前进程的所有非守护线程 全部结束后,当前进程才能结束。
Demo2: 进程创建守护线程
#!/usr/bin/python3
# FileName: daemonThread.py
# Author: lxw
# Date: 2016-02-25 import threading
import time def show(num):
time.sleep(3)
print("In show(): {0}".format(num))
with open("/home/lxw/Documents/NOTE", "w") as f:
f.write("hello") def main():
th = threading.Thread(target=show, args=(1,))
th.setDaemon(True)
th.start()
print("main() over. Program exit!") if __name__ == '__main__':
main()
else:
print("Being imported as a module.")
运行结果:
lxw Documents$ ls
daemonThread.py NOTE ThreadPool_Python
lxw Documents$ rm NOTE
lxw Documents$ ls
daemonThread.py ThreadPool_Python
lxw Documents$ python3 daemonThread.py
main() over. Program exit!
lxw Documents$ ls
daemonThread.py ThreadPool_Python
从运行结果我们可以看到,当前进程的守护线程 并没有结束,但当前进程仍然可以退出。并且当前进程结束后,该进程的守护线程也会结束,不会继续运行。
也就是说进程退出并不考虑守护线程,只考虑非守护线程。
通过这两段代码的验证,我们可以得出本文标题的答案:进程结束后,该进程的守护线程也会结束,不会继续运行。
Reference:
1. [Java基础] java的守护线程与非守护线程 虽然这篇文章讲的是Java的,但和Python也是通用的。
守护线程并非虚拟机内部可以提供,用户也可以自行的设定守护线程,方法:public final void setDaemon(boolean on) ;但是有几点需要注意:
1). thread.setDaemon(true)必须在thread.start()之前设置,否则会跑出一个 IllegalThreadStateException异常。你不能把正在运行的常规线程设置为守护线程。(备注:这点与守护进程有着明显的区别, 守护进程是创建后,让进程摆脱原会话的控制+让进程摆脱原进程组的控制+让进程摆脱原控制终端的控制;所以说寄托于虚拟机的语言机制跟系统级语言有着本质上面的区别)
2). 在Daemon线程中产生的新线程也是Daemon的。(这一点又是有着本质的区别了:守护进程fork()出来的子进程不再是守护进程,尽管它把父进程的进程相关信息复制过去了,但是子进程的进程的父进程不是init进程,所谓的守护进程本质上说就是“父进程挂掉,init收养,然后 文件0,1,2都是/dev/null,当前目录到/”)
2. 这篇博文的评论
Does Daemon Thread Exit with Main Thread?的更多相关文章
- Sub Thread to update main Thread (UI) 2
Sub Thread to update main Thread (UI) 2 Handler.post(somethread); Handler.sendMessage("Msg&quo ...
- Sub Thread to update main Thread (UI)
Sub Thread to update main Thread (UI) main Thread : A has Hander.HandleMessage() to process the & ...
- tensorflow_目标识别object_detection_api,RuntimeError: main thread is not in main loop,fig = plt.figure(frameon=False)_tkinter.TclError: no display name and no $DISPLAY environment variable
最近在使用目标识别api,但是报错了: File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/script_o ...
- Why NSAttributedString import html must be on main thread?
The HTML importer should not be called from a background thread (that is, the options dictionary inc ...
- 13、主线程任务太多导致异常退出(The application may be doing too much work on its main thread)
今天花费了一天的时间来解决这个bug. 这种在程序运行期间出现的问题比较棘手,如果再没有规律的话就更难解决. 还好这个bug是由规律的,也就是说在程序执行半个小时左右后就会因为此异常而导致程序退出:那 ...
- reloadData should be in main thread
reloadData should be called in main thread, so if you call it in work thread, you should call it as ...
- main thread starting…
例的结果,下面的: main thread starting- Thrad 2 staring- Thrad 2 end- Thrad 4 staring- Thrad 4 end- Thrad 1 ...
- APP崩溃提示:This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
崩溃输出日志 2017-08-29 14:53:47.332368+0800 HuiDaiKe[2373:1135604] This application is modifying the auto ...
- Ajax.html:35 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org
AJAX的容易错误的地方 Ajax.html:35 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated ...
随机推荐
- python中的字典 和 集合
python中字典是一种key-value的数据类型 字典的特性: 1.无序的 2.key必须的唯一的,so,字典天生去重 语法: 增加 修改 删除 查找 多级字典嵌套及操作 字典的其他用法 #set ...
- NPOI例子
例子链接:http://www.cnblogs.com/atao/tag/NPOI/default.html?page=1
- 漫游Kafka设计篇之消息传输的事务定义(5)
之前讨论了consumer和producer是怎么工作的,现在来讨论一下数据传输方面.数据传输的事务定义通常有以下三种级别: 最多一次: 消息不会被重复发送,最多被传输一次,但也有可能一次不传输. 最 ...
- MySQL [Err]1449 : The user specified as a definer ('root'@'%') does not exist
权限问题:授权 给 root 所有sql 权限 mysql> grant all privileges on *.* to root@"%" identified by &q ...
- 第四篇:使用 CUBLAS 库给矩阵运算提速
前言 编写 CUDA 程序真心不是个简单的事儿,调试也不方便,很费时.那么有没有一些现成的 CUDA 库来调用呢? 答案是有的,如 CUBLAS 就是 CUDA 专门用来解决线性代数运算的库. 本文将 ...
- Android 切换主题 (二)
Android 切换主题 (二) 背景 我原来写过一篇文章关于 android 切换主题的文章 -- Android 切换主题以及换肤的实现 , 里面介绍了如何使用 setTheme() 来切换主题, ...
- keepalive和脑裂问题
keepalive keepalive起初专门为lvs负载均衡软件设计的,用来管理监控lvs集群系统中各个服务节点的状态,后来又加入了可以实现高可用的vrrp功能. keepalive软件通过vrrp ...
- 利用MFC实现浏览器的定制与扩展(JavaScript与C++交互)
原文地址:http://www.vckbase.com/document/viewdoc/?id=1486 浏览器的定制与扩展 作者:李汉鹏 下载源代 码 本文分如下章节: 前 言 在 ...
- IDEA整合日志框架Log4j2+Slf4j详细配置过程
日志框架这么多,他们之间到底是什么关系呢?笼统的讲就是slf4j是一系列的日志接口,而log4j2.logback是具体实现了接口功能的日志框架.现在的主流日志接口都使用slf4j,而日志的实现就见仁 ...
- 170324、Spring 处理器和Resource
1.Spring 框架允许开发者使用两种后处理器扩展 IoC 容器,这两种后处理器扩展 IoC 容器,这两种后处理器可以后处理 IoC 容器本身,或对容器中所有的 Bean 进行后处理.IoC 容器还 ...