python多线程的几种情形分析-三种情况
情形一:默认情况
默认情况,只开启线程,那么,主线程结束,其他子线程可能还没结束。
只使用t=threading.Thead(target=fun),t.start()。
import threading
import time def run():
time.sleep(2)
print('当前线程的名字是: ', threading.current_thread().name)
time.sleep(2) if __name__ == '__main__': start_time = time.time() print('这是主线程:', threading.current_thread().name)
thread_list = []
for i in range(5):
t = threading.Thread(target=run)
thread_list.append(t) for t in thread_list:
t.start() print('主线程结束!' , threading.current_thread().name)
print('一共用时:', time.time()-start_time)
例子
情形二:守护线程(后台线程)
含义:主线程一结束,其他守护线程跟着一起结束。
比情形一多了一个t.setDaemon(True),默认情况下是False(前台线程),注意,setDaemon(True)必须在start()前面。
import threading
import time def run(): time.sleep(2)
print('当前线程的名字是: ', threading.current_thread().name)
time.sleep(2) if __name__ == '__main__': start_time = time.time() print('这是主线程:', threading.current_thread().name)
thread_list = []
for i in range(5):
t = threading.Thread(target=run)
thread_list.append(t) for t in thread_list:
t.setDaemon(True)
t.start() print('主线程结束了!' , threading.current_thread().name)
print('一共用时:', time.time()-start_time)
例子
情形三:线程同步join
含义:主线程结束,但需要等待其他子线程的执行,下面的例子,跟没有使用线程没有区别,顺序执行,必须等待第一个join执行完成才能执行下一个。
与setDaemon相反的是join要写在start()之后,当然,功能也跟setDeamon相反。
import threading
import time def run(): time.sleep(2)
print('当前线程的名字是: ', threading.current_thread().name)
time.sleep(2) if __name__ == '__main__': start_time = time.time() print('这是主线程:', threading.current_thread().name)
thread_list = []
for i in range(5):
t = threading.Thread(target=run)
thread_list.append(t) for t in thread_list:
t.setDaemon(True)
t.start() for t in thread_list:
t.join() print('主线程结束了!' , threading.current_thread().name)
print('一共用时:', time.time()-start_time)
例子1
import threading
import time def run(): time.sleep(2)
print('当前线程的名字是: ', threading.current_thread().name)
time.sleep(2) if __name__ == '__main__': start_time = time.time() print('这是主线程:', threading.current_thread().name)
thread_list = []
for i in range(5):
t = threading.Thread(target=run)
thread_list.append(t) for t in thread_list:
t.setDaemon(True)
t.start() for t in thread_list:
t.join(0.2) print('主线程结束了!' , threading.current_thread().name)
print('一共用时:', time.time()-start_time)
例子2
注意:例子二给join传一个0.2的值,那么子程序一共执行1秒钟。可以看到,子线程没完全执行完毕,如果将这个值设置成0.4或者0.5又会如何,大家自己试一试。
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 range(4):
t =threading.Thread(target=action,args=(i,))
t.setDaemon(True)
t.start()
t.join() 这样对的程序只能顺序执行,每个线程都被上一个线程的join阻塞,使得“多线程”失去了多线程意义。
join的错误用法,新手请注意
python多线程的几种情形分析-三种情况的更多相关文章
- 13 数组 Java内存分析 三种初始化
Java内存分析 三种初始化 静态初始化 //静态初始化 创建+赋值 int[] a = {1,2,3}; Man[] mans = {new Man(1,1),new Man(2,2)}; 动态初始 ...
- 1.Git起步-Git的三种状态以及三种工作区域、CVCS与DVCS的区别、Git基本工作流程
1.Git基础 版本控制系统是一种用于记录一个或多个文件内容变化,以便将来查阅恢复特定版本修订情况的系统. Git是一种分布式版本控制系统(Distributed Version Control Sy ...
- python 判断变量是否是 None 的三种写法
代码中经常会有变量是否为None的判断,有三种主要的写法:第一种是`if x is None`:第二种是 `if not x:`:第三种是`if not x is None`(这句这样理解更清晰`if ...
- java多线程二之线程同步的三种方法
java多线程的难点是在:处理多个线程同步与并发运行时线程间的通信问题.java在处理线程同步时,常用方法有: 1.synchronized关键字. 2.Lock显示加锁. 3.信号量Se ...
- 官网安装Python包太慢?教你三种下载安装方式-PiP、conda、轮子,教你三种Pytorch的下载安装方式,保证你再也不用出现Error
上一期我们介绍了CUDA下载安装以及其总结,这一期教大家如何在Anaconda中使用CUDA来进行加速.神经网络依赖cuDNN的下载安装,以及下载和安装Pytorch-GPU安装包的三种方式(cond ...
- 以用户名注册来分析三种Action获取数据的方式
1.注入属性 直接注入属性: public String userName; public String getUserName() { return userName; } public void ...
- Linux三种网络-vmware三种网络模式
Host-Only 桥接 NAT VMware虚拟机三种联网方法及原理 一.Brigde——桥接:默认使用VMnet0 1.原理: Bridge 桥"就是一个主机,这个机器拥有两块网卡,分别 ...
- Qt 设置背景图片3种方法(三种方法:QPalette调色板,paintEvent,QSS)
方法1. setStylSheet{"QDialog{background-image:url()"}} //使用styleSheet 这种方法的好处是继承它的dialog都会自 ...
- python利用(threading,ThreadPoolExecutor.map,ThreadPoolExecutor.submit) 三种多线程方式处理 list数据
需求:在从银行数据库中取出 几十万数据时,需要对 每行数据进行相关操作,通过pandas的dataframe发现数据处理过慢,于是 对数据进行 分段后 通过 线程进行处理: 如下给出 测试版代码,通过 ...
随机推荐
- luoguP3391[模板]文艺平衡树(Splay) 题解
链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...
- Matplotlib基础使用
matplotlib 一.Matplotlib基础知识 Matplotlib中的基本图表包括的元素 x轴和y轴 axis 水平和垂直的轴线 x轴和y轴刻度 tick 刻度标示坐标轴的分隔,包括最小刻度 ...
- Day9---Python的集合类
集合类 1.生成办法: 可使用{.....} 或者set()生成集合,例如 a = {23,214,34,324,234,34} #这里的集合就是数学上的集合a = set('dsfasfsdf') ...
- SpringBoot-技术专区-详细打印启动时异常堆栈信息
SpringBoot在项目启动时如果遇到异常并不能友好的打印出具体的堆栈错误信息,我们只能查看到简单的错误消息,以致于并不能及时解决发生的问题,针对这个问题SpringBoot提供了故障分析仪的概念( ...
- 使用Anaconda3安装tensorflow,opencv,使其可以在spyder中运行
使用Anaconda5.0.0 1.首选无论你是在cmd键入python,还是在Anaconda Prompt键入python,显示的都是Python3.6.然而在Spyder(tensorflow) ...
- Windows server 2012/2016系统安装zabbix3.2客户端
一. 上传zabbix客户端文件 将zabbix_agents_3.2.0.win.zip文件上传至服务器的指定路径. 二. 解压并修改相关信息 解压已上传的客户端文件,在co ...
- 实现多线程的三种方法:Thread、Runnable和Callable
继承Thread类,重写run()方法 步骤: (1) 定义类继承Thread类 (2) 复写Thread类中的run方法. (3) 调用线程的start方法 (start方法有两种含义:1. 启动多 ...
- Go's Declaration Syntax
Introduction Newcomers to Go wonder why the declaration syntax is different from the tradition estab ...
- Synchronized和ReentranLock的比较
并发编程最容易遇到的问题就是就是安全问题,因此解决方式有两种 使用同步方法或同步代码块(Synchronized关键字) 使用锁机制(ReentranLock) 同步方法和同步代码块(Synchron ...
- svnkit 异常:Exception in thread "main" org.tmatesoft.svn.core.SVNException: svn: E200030: SQLite error
https://stackoverflow.com/questions/16063105/org-tmatesoft-sqljet-core-sqljetexception-busy-error-co ...