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 ...
随机推荐
- JavaBeans wiki 摘译
20161013最新提示:既然来到这了,为什么不看看 JavaBeans 官方文档学习 ? 鉴于Spring的beans包遵守JavaBean specs,有必要认真研究下JavaBean specs ...
- 异常之*** buffer overflow detected ***
*** buffer overflow detected *** 是sprintf()超出buff大小
- 【cf489】D. Unbearable Controversy of Being(暴力)
http://codeforces.com/contest/489/problem/D 很显然,我们只需要找对于每个点能到达的深度为3的点的路径的数量,那么对于一个深度为3的点,如果有a种方式到达,那 ...
- media wiki run on nginx
1. 环境安装: nginx安装 nginx-1.5.7 php安装 PHP 5.4.10 (cli) (built: Jul 30 2014 16:45:08) mysql安装 Ver 14.14 ...
- bootstrap基础学习五篇
bootstrap表格 Bootstrap 提供了一个清晰的创建表格的布局.下表列出了 Bootstrap 支持的一些表格元素: 标签 描述 <table> 为表格添加基础样式. < ...
- 基于WebSocket实现网页版聊天室
WebSocket ,HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议,其使用简单,应用场景也广泛,不同开发语言都用种类繁多的实现,仅Java体系中,Tomcat,Jetty,Sp ...
- jconsole监控tomcat
一.专业术语 GC垃圾回收机制:当需要分配的内存空间不再使用的时候,JVM将调用垃圾回收机制来回收内存空间. JMX(Java Management Extensions,即Java管理扩展)是一个为 ...
- iOS面试题--网络--如何处理多个网络请求的并发的情况
如何处理多个网络请求的并发的情况 一.概念 1.并发 当有多个线程在操作时,如果系统只有一个CPU,则它根本不可能真正同时进行一个以上的线程,它只能把CPU运行时间划分成若干个时间段,再将时间 段分配 ...
- Thinkphp中如何书写按照指定字段同步更新的ORM
群友提出一个问题,如何在更新某个字段的时候同步另一个字段数据过来,即 update table set column1 =column2 where xxx 写原生SQL当然可行,不过既然有ORM那就 ...
- iOS开发之--为PCH文件添加绝对路径
要想设置PCH的相对路径,首先我们需要去查看绝对路径. 相对路径 点击PCH文件,Xcode的右侧会显示PCH的属性.这里我们可以获取到PCH的绝对路径.从工程的路径开始,前面使用$(SRCROOT) ...