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发现数据处理过慢,于是 对数据进行 分段后 通过 线程进行处理: 如下给出 测试版代码,通过 ...
随机推荐
- 使用bootstrap制作网站导航
除了制作选项卡和下拉菜单,bootstrap还能编写出美观的网站导航栏 一.仿知乎导航栏 <body> <nav class="navbar navbar-default ...
- Codeforces 1000E We Need More Bosses (边双连通+最长链)
<题目链接> 题目大意:给定一个$n$个节点$m$条边的无向图,问你对任意两点,最多有多少条特殊边,特殊边指删除这条边后,这两个点不能够到达. 解题分析: 特殊变其实就是指割边,题意就是问 ...
- 03.Linux-CentOS系统user用户改密码问题
问题:[user@localhost ~]$ passwdChanging password for user user.Changing password for user.(current) UN ...
- 让docker容器使用主机系统时间(挂入/etc/localtime)
-v挂入这个文件就可以了: -v /etc/localtime:/etc/localtime:ro
- 条款2:尽量使用const ,enum,inline替换define
宁可使用编译器而不用预处理器 假设我们使用预处理器: #define ABC 1.56 这标识符ABC也许编译器没看到,也许它在编译器处理源码前就被预处理器移走了,于是“标识符”ABC没有进入标识符列 ...
- egrep 或 多个连续字符测数字
.TXT 4-6是ABC 或者 4-6是 0-9
- "Unable to locate package lrzsz"的解决办法
某天安装一些常用软件,比如lrzsz的时候出错了 $ sudo apt-get install lrzsz Reading package lists... Done Building depende ...
- html5 实例渐变
代码实例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 通过ssh访问虚拟机中的ubuntu系统
首先把 network 连接方式由 NAT 改为 Bridge Adapter,这样虚拟机中的 ubuntu 就可以有独立的 IP 地址. 安装 openssh: sudo apt-get insta ...
- postman-Runner
postman Runner配置 preview查看参数