python threading模块中的join()方法呵setDeamon()方法的一些理解
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python threading模块中的join()方法呵setDeamon()方法的一些理解,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3024字,纯文字阅读大概需要5分钟。
内容图文
![python threading模块中的join()方法呵setDeamon()方法的一些理解](/upload/InfoBanner/zyjiaocheng/847/00d57985818b49fd8c980ed6f690a274.jpg)
之前用多线程的时候看见了很多文章,比较常用的大概就是join()和setDeamon()了。
先说一下自己对join()的理解吧:
def join(self, timeout=None):
"""Wait until the thread terminates.
This blocks the calling thread until the thread whose join() method is
called terminates -- either normally or through an unhandled exception
or until the optional timeout occurs.
When the timeout argument is present and not None, it should be a
floating point number specifying a timeout for the operation in seconds
(or fractions thereof). As join() always returns None, you must call
isAlive() after join() to decide whether a timeout happened -- if the
thread is still alive, the join() call timed out.
When the timeout argument is not present or None, the operation will
block until the thread terminates.
A thread can be join()ed many times.
join() raises a RuntimeError if an attempt is made to join the current
thread as that would cause a deadlock. It is also an error to join() a
thread before it has been started and attempts to do so raises the same
exception.
"""
源码的__doc__其实说的很清楚,join()会阻塞调用该线程的线程,知道该线程结束(This blocks the calling thread
until the thread whose join() method is called terminates)。
注意点:
#coding:utf-8 import threading import time def action(arg): time.sleep(1) print 'sub thread start!the thread name is:%s ' % threading.currentThread().getName() print 'the arg is:%s ' %arg time.sleep(1) #不正确写法,会导致多线程顺序执行,失去了多线程的意义 for i in xrange(4): t =threading.Thread(target=action,args=(i,)) t.setDaemon(True) t.start() t.join() #正确写法 thread_list = [] #线程存放列表 for i in xrange(4): t =threading.Thread(target=action,args=(i,)) t.setDaemon(True) thread_list.append(t) for t in thread_list: t.start() for t in thread_list: t.join() print 'main_thread end!'
下面再说一下setDeamon()吧:
其实主线程并不会结束setDeamon(True)的线程,而是当主线程执行完毕之后不会再去关注setDeamon(True)的线程。
所以setDeamon(True)的线程的结果是我们无法获取到的,类似于爱咋咋地?不管你输出什么,我都不看,主线程跑
完就结束整个python process。
而setDeamon(False)的线程会一直受到主线程的关注,就算主线程跑完了也会等setDeamon(False)的线程跑完然后
再结束整个python process。
所以说,就算setDeamon(True)的线程在主线程之后跑完,但如果在setDeamon(False)的线程之前跑完的话,也是会
输出结果的,而不是被所谓的主线程结束就杀死setDeamon(False)的线程。
附上我的调试代码,
# -*- coding:utf-8 -*- import threading import time stime = time.time() def action(arg): time.sleep(1) print ('the arg is:%s,time is %s ' % (arg, time.time())) def action1(arg): time.sleep(1) print ('the arg is:%s,time is %s ' % (arg, time.time())) thread_list = [] # 线程存放列表 for i in [777, 888]: t = threading.Thread(target=action, args=(i,)) t.setDaemon(False) thread_list.append(t) for i in range(10): t = threading.Thread(target=action1, args=(i,)) t.setDaemon(True) thread_list.append(t) for t in thread_list: t.start() print('**************') etime = time.time() print('time cost is %s' % (etime - stime))
内容总结
以上是互联网集市为您收集整理的python threading模块中的join()方法呵setDeamon()方法的一些理解全部内容,希望文章能够帮你解决python threading模块中的join()方法呵setDeamon()方法的一些理解所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。