之前用多线程的时候看见了很多文章,比较常用的大概就是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()方法的一些理解的更多相关文章

  1. Java8新特性(一)_interface中的static方法和default方法

    什么要单独写个Java8新特性,一个原因是我目前所在的公司用的是jdk8,并且框架中用了大量的Java8的新特性,如上篇文章写到的stream方法进行过滤map集合.stream方法就是接口Colle ...

  2. JS中的call()方法和apply()方法用法总结

    原文引自:https://blog.csdn.net/ganyingxie123456/article/details/70855586 最近又遇到了JacvaScript中的call()方法和app ...

  3. Hibernate中Session.get()方法和load()方法的详细比较

    一.get方法和load方法的简易理解  (1)get()方法直接返回实体类,如果查不到数据则返回null.load()会返回一个实体代理对象(当前这个对象可以自动转化为实体对象),但当代理对象被调用 ...

  4. js中的splice方法和slice方法简单总结

    slice:是截取用的 splice:是做删除 插入 替换用的 slice(start,end): 参数: start:开始位置的索引 end:结束位置的索引(但不包含该索引位置的元素) 例如: va ...

  5. JS中的call()方法和apply()方法用法总结(挺好 转载下)

    最近又遇到了JacvaScript中的call()方法和apply()方法,而在某些时候这两个方法还确实是十分重要的,那么就让我总结这两个方法的使用和区别吧. 1. 每个函数都包含两个非继承而来的方法 ...

  6. TP框架中的A方法和R方法

    ThinkPHP 跨模块调用操作方法(A方法与R方法) 跨模块调用操作方法 前面说了可以使用 $this 来调用当前模块内的方法,但实际情况中还经常会在当前模块调用其他模块的方法.ThinkPHP 内 ...

  7. Mapper类/Reducer类中的setup方法和cleanup方法以及run方法的介绍

    在hadoop的源码中,基类Mapper类和Reducer类中都是只包含四个方法:setup方法,cleanup方法,run方法,map方法.如下所示: 其方法的调用方式是在run方法中,如下所示: ...

  8. java 中的set方法和get方法的理解

    get的意思是获取,set的意思是设置. get方法和set方法是实现类的封装访问的很好的工具. 当类中的变量设为private 时,他的意思就是说,只能通过自身和子类的访问,但是对于别的其他的类来说 ...

  9. java8新特性:interface中的static方法和default方法

    java8中接口有两个新特性,一个是静态方法,一个是默认方法. static方法 java8中为接口新增了一项功能:定义一个或者多个静态方法. 定义用法和普通的static方法一样: public i ...

随机推荐

  1. day07数据类型的相互转化,字符编码

    复习 ''' 1.深浅拷贝 ls = [1, 'a', [10]] 值拷贝:直接赋值 ls1 = ls, ls中的任何值发生改变,ls1中的值都会随之改变 浅拷贝:通过copy()方法 ls2 = l ...

  2. ARDUINO入门按键通信试验

    1.1按键实验 1.需要学习的知识: 1) Arduino 的输入口配置方法,配置函数的用法 通过pinMode()函数,可以将ADUINO的引脚配置(INPUT)输入模式 2) 搞懂什么是抖动 机械 ...

  3. scrapy 爬虫的暂停与重启

    暂停爬虫项目 首先在项目目录下创建一个文件夹用来存放暂停爬虫时的待处理请求url以及其他的信息.(文件夹名称:job_info) 在启动爬虫项目时候用pycharm自带的终端启动输入下面的命令: sc ...

  4. .NET/C# 反射的的性能数据,以及高性能开发建议(反射获取 Attribute 和反射调用方法)——转载

    原文链接:https://blog.walterlv.com/post/dotnet-high-performance-reflection-suggestions.html ***** 大家都说反射 ...

  5. 论文笔记:Decoders Matter for Semantic Segmentation: Data-Dependent Decoding Enables Flexible Feature Aggregation

    Decoders Matter for Semantic Segmentation: Data-Dependent Decoding Enables Flexible Feature Aggregat ...

  6. Markdown语法进阶

    tip:基本都是通过html格式实现的. 插入音频 插入音乐 在网易云音乐里找生成外连接,复制过来就OK了,可惜的是很多都不能生成外联连接.如果想自动播放,可以把auto改成1. 插入视频 直接引用在 ...

  7. 对java中路径的一些理解

    开始前先贴一下项目结构 public class TestLocation { @Test public void test1(){ String s1 = Objects.requireNonNul ...

  8. 6.4 SpringData JPA的使用

    引言:该文档是参考尚硅谷的关于springboot教学视屏后整理而来.当然后面还加入了一些自己从网上收集整理而来的案例! 一.SpringData JPA初步使用 1. springdata简介 2. ...

  9. 使用jQuery修改带有!important 的强制样式

    我们习惯性的会用这种方法进行修改: $(".test").css("width","100px !important"); 但是往往是修改失 ...

  10. ionic 在windows环境下更换logo和加载图片的问题

    做用自己的电脑做ionic项目时,更换logo和加载图片时,无论使用哪种命令,发现都上传不了,并且报错 最后发现,需要将 icon和splash两个文件改为.ai格式才能上传成功. 这是最终生成后的文 ...