threading.Thread.setDaemon()方法
2014-05-08 16:00
405 查看
例:
官档上是这样解释的:
个人理解:
setDaemon(True)将线程声明为守护线程,必须在start()
方法调用之前设置,如果不设置为守护线程程序会被无限挂起。
The entire Python program exits when no alive non-daemon threads are left
上面这句话的意思是:当没有存活的非守护进程时,整个python程序才会退出。
也就是说:如果主线程执行完以后,还有其他非守护线程,主线程是不会退出的,会被无限挂起;必须将线程声明为守护线程之后,如果队列中的数据运行完了,那么整个程序想什么时候退出就退出,不用等待。
参考:http://topic.csdn.net/t/20060115/00/4517316.html
for i in range(num_threads): worker = Thread(target=pinger, args=(i, queue)) worker.setDaemon(True) worker.start() |
setDaemon() Old API for daemon. A boolean value indicating whether this thread is a daemon thread (True) or not (False). This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False. The entire Python program exits when no alive non-daemon threads are left. |
setDaemon(True)将线程声明为守护线程,必须在start()
方法调用之前设置,如果不设置为守护线程程序会被无限挂起。
The entire Python program exits when no alive non-daemon threads are left
上面这句话的意思是:当没有存活的非守护进程时,整个python程序才会退出。
也就是说:如果主线程执行完以后,还有其他非守护线程,主线程是不会退出的,会被无限挂起;必须将线程声明为守护线程之后,如果队列中的数据运行完了,那么整个程序想什么时候退出就退出,不用等待。
参考:http://topic.csdn.net/t/20060115/00/4517316.html
相关文章推荐
- Thread的setDaemon(true)方法的作用
- Thread的setDaemon(true)方法的作用
- 【Java基础】Thread setDaemon 方法
- Thread的setDaemon(true)方法的作用
- Thread的setDaemon(true)方法的作用
- Thread的setDaemon(true)方法的作用
- Thread setDaemon 方法
- Java.线程.Thread类.Runnable接口.start().setDaemon().join()
- Thread.setDaemon的用法
- Thread.setDaemon设置说明
- Thread.setDaemon详解
- [Python]Threading.Thread之Daemon线程
- Thread.setDaemon详解
- Python中threading的join和setDaemon的区别及用法[例子]
- Python中threading的join和setDaemon的区别及用法[例子]
- python中thread的setDaemon、join的用法
- thread.setDaemon服务线程
- Python中threading的join和setDaemon的区别及用法 例子
- 线程调度四(setDaemon方法的使用)
- Thread.setDaemon详解