几个事实

1 python 默认参数创建线程后,不管主线程是否执行完毕,都会等待子线程执行完毕才一起退出,有无join结果一样

2 如果创建线程,并且设置了daemon为true,即thread.setDaemon(True), 则主线程执行完毕后自动退出,不会等待子线程的执行结果。而且随着主线程退出,子线程也消亡。

3 join方法的作用是阻塞,等待子线程结束,join方法有一个参数是timeout,即如果主线程等待timeout,子线程还没有结束,则主线程强制结束子线程。

4 如果线程daemon属性为False, 则join里的timeout参数无效。主线程会一直等待子线程结束。

5 如果线程daemon属性为True, 则join里的timeout参数是有效的, 主线程会等待timeout时间后,结束子线程。此处有一个坑,即如果同时有N个子线程join(timeout),那么实际上主线程会等待的超时时间最长为 N * timeout, 因为每个子线程的超时开始时刻是上一个子线程超时结束的时刻。

测试代码

import threading,time

def func():
print "start thread time: ",time.strftime('%H:%M:%S')
time.sleep(3)
print "stop thread time: ",time.strftime('%H:%M:%S') thread_list = []
for i in range(3):
t1 = threading.Thread(target=func)
#t1.setDaemon(True) thread_list.append(t1) for r in thread_list:
r.start() for t in thread_list:
#t.join(1)
t.join()
print "stop main thread" ###子线程如果设置了t.join(timeout),则根据timeout的不同,结果会不同,前提是设置了setDaemon(True),否则join的timeout是没效的 #设置了setDaemon(True),但是没设置t.join()的运行结果:
#start thread time: 17:25:29
#start thread time: 17:25:29
#start thread time: 17:25:29
#stop main thread #加了t1.setDaemon(True),并且设置了超时时间t.join(1)的运行结果:
#start thread time: 17:12:24
#start thread time: 17:12:24
#start thread time: 17:12:24
#stop main thread #没加t1.setDaemon(True),并且设置了超时时间t.join(1)的运行结果,不过因为setDaemon的参数不是True所以就算设置了超时时间也没用:
#start thread time: 17:13:28
#start thread time: 17:13:28
#start thread time: 17:13:28
#stop main thread
#stop thread time: 17:13:31
#stop thread time: 17:13:31
#stop thread time: 17:13:31 #没加t1.setDaemon(True),但是设置了t.join(),没有超时时间的阻塞的运行结果:
#start thread time: 17:16:12
#start thread time: 17:16:12
#start thread time: 17:16:12
#stop thread time: 17:16:15
#stop thread time: 17:16:15
#stop thread time: 17:16:15
#stop main thread #即没有设置setDaemon(True),也没有设置join()的运行结果:
#start thread time: 17:22:25
#start thread time: 17:22:25
#start thread time: 17:22:25
#stop main thread
#stop thread time: 17:22:28
#stop thread time: 17:22:28
#stop thread time: 17:22:28
总结:
如果想让子进程正常的运行结束(子进程中所有的内容都运行了),则如果设置join(timeout)的话,前提是设置setDaemon(True),且setDaemon的参数为True,且join(timeout)的超时时间必须大于子进程执行所需的时间,不然没等子进程运行结束就超
时退出了.或者直接设置join()不带超时时间,也不用设置setDaemon(True)了

python线程join的更多相关文章

  1. python线程join方法

    转载:http://www.cnblogs.com/cnkai/p/7504980.html Python多线程与多进程中join()方法的效果是相同的. 下面仅以多线程为例: 首先需要明确几个概念: ...

  2. Python线程join和setDaemon

    看一下线程的setDaemon()方法 import time import threading import ctypes import inspect def sayHello(): for i ...

  3. python网络编程--线程join和Daemon(守护进程)

    一:什么情况下使用join join([timeout])调用join函数会使得主调线程阻塞,直到被调用线程运行结束或超时. 参数timeout是一个数值类型,用来表示超时时间,如果未提供该参数,那么 ...

  4. Python并发编程04 /多线程、生产消费者模型、线程进程对比、线程的方法、线程join、守护线程、线程互斥锁

    Python并发编程04 /多线程.生产消费者模型.线程进程对比.线程的方法.线程join.守护线程.线程互斥锁 目录 Python并发编程04 /多线程.生产消费者模型.线程进程对比.线程的方法.线 ...

  5. 线程join理解

    1.python默认参数创建线程后,不管主线程是否执行完毕,都会等待子线程执行完毕才一起退出,有无join结果一样 2.如果创建线程,并且设置了daemon为true,即thread.setDaemo ...

  6. python——线程与多线程进阶

    之前我们已经学会如何在代码块中创建新的线程去执行我们要同步执行的多个任务,但是线程的世界远不止如此.接下来,我们要介绍的是整个threading模块.threading基于Java的线程模型设计.锁( ...

  7. [python] 线程简介

    参考:http://www.cnblogs.com/aylin/p/5601969.html 我是搬运工,特别感谢张岩林老师! python 线程与进程简介 进程与线程的历史 我们都知道计算机是由硬件 ...

  8. [python] 线程

    来源:田飞雨 链接:http://www.jianshu.com/p/12cd213a93bf 虽然python中由于GIL的机制致使多线程不能利用机器多核的特性,但是多线程对于我们理解并发模型以及底 ...

  9. Python 线程(threading) 进程(multiprocessing)

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

随机推荐

  1. POJ 2438 Children’s Dining (哈密顿图模板题之巧妙建反图 )

    题目链接 Description Usually children in kindergarten like to quarrel with each other. This situation an ...

  2. ECMAScript 6中的var,let,const

    var的变量提升 console.log(a); //输出undefined ; 他的实际执行顺序是: var a: console.log(a); a= 这就是var的变量提升 const命令的用法 ...

  3. APScheduler API -- apscheduler.triggers.date

    apscheduler.triggers.date API Trigger alias for add_job(): date class apscheduler.triggers.date.Date ...

  4. jQuery选择器——(三)

    1.基本元素选择器 id选择器:$(“#id名称”); 元素选择器:$(“元素名称”); 类选择器:$(“.类名”); 通配符:* 多个选择器共用(并集) 2.层级选择器 ancestor desce ...

  5. 基于NIO的同步非阻塞编程完整案例,客户端发送请求,服务端获取数据并返回给客户端数据,客户端获取返回数据

    这块还是挺复杂的,挺难理解,但是多练几遍,多看看研究研究其实也就那样,就是一个Selector轮询的过程,这里想要双向通信,客户端和服务端都需要一个Selector,并一直轮询, 直接贴代码: Ser ...

  6. React-Native 之 环境配置和简单使用

    # 前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会 ...

  7. 最长子串(Leetcode-3 Longest Substring Without Repeating Characters)

    Question: Given a string, find the length of the longest substring without repeating characters. Exa ...

  8. angular架构

    angular架构包括以下部分: 1.模块 2.组件 3.模板 4.元数据 5.数据绑定 6.指令 7.服务 8.依赖注入 9.动画 10.变更检测 11.事件 12.表单 13.HTTP 14.生命 ...

  9. 缓存数据库-redis数据类型和操作(hash)

    一:Redis 哈希(Hash) Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象. Redis 中每个 hash 可以存储 232 - 1 ...

  10. Python_oldboy_自动化运维之路_全栈考试(七)

    1. 计算100-300之间所有能被3和7整除的所有数之和 # -*- coding: UTF-8 -*- #blog:http://www.cnblogs.com/linux-chenyang/ c ...