Threadpool python3
from concurrent.futures import ThreadPoolExecutor,ALL_COMPLETED,wait,as_completedimport time
def addNum(x,y): # print("test 测试阻塞{}".format(time.asctime())) time.sleep(1) return x+ydef executors(): list1=[i for i in range(1000)] list2=[j for j in range(1000,2000)] executor=ThreadPoolExecutor(max_workers=4) all_task=[executor.submit(addNum,x,y) for x,y in zip(list1,list2)] # 任务没完成的时候as_completed会阻塞,先完成的会先yield,按谁先完成输出 st=time.perf_counter() for f in as_completed(all_task): res=f.result() print("这是计算的结果{}--{}".format(res,time.asctime())) end=time.perf_counter()-st print(end)def mapExecutor(): """按顺序list的,即使先完成""" executor=ThreadPoolExecutor(max_workers=4) list1=[i for i in range(100)] list2=[j for j in range(100,200)] for res in executor.map(addNum,list1,list2): print("this is res {}--{}".format(res,time.asctime())) print("main finish")def waitExec(): executor=ThreadPoolExecutor(max_workers=4) list1=[i for i in range(10)] list2=[j for j in range(10,20)] all_task=[executor.submit(addNum,x,y) for x,y in zip(list1,list2)] #当所有任务完成后才输出结果,不然就阻塞 wait(fs=all_task,timeout=None,return_when=ALL_COMPLETED) print("主线程finish")if __name__ == '__main__': mapExecutor()
Threadpool python3的更多相关文章
- python3 线程池-threadpool模块与concurrent.futures模块
多种方法实现 python 线程池 一. 既然多线程可以缩短程序运行时间,那么,是不是线程数量越多越好呢? 显然,并不是,每一个线程的从生成到消亡也是需要时间和资源的,太多的线程会占用过多的系统资源( ...
- Python3 与 C# 并发编程之~ 进程篇
上次说了很多Linux下进程相关知识,这边不再复述,下面来说说Python的并发编程,如有错误欢迎提出- 如果遇到听不懂的可以看上一次的文章:https://www.cnblogs.com/dot ...
- Python3爬虫系列:理论+实验+爬取妹子图实战
Github: https://github.com/wangy8961/python3-concurrency-pics-02 ,欢迎star 爬虫系列: (1) 理论 Python3爬虫系列01 ...
- Python3 与 C# 并发编程之~ 线程篇
2.线程篇¶ 在线预览:https://github.lesschina.com/python/base/concurrency/3.并发编程-线程篇.html 示例代码:https://gith ...
- Python3 与 C# 网络编程之~ 网络基础篇
最新版本查看:https://www.cnblogs.com/dotnetcrazy/p/9919202.html 入门篇 官方文档:https://docs.python.org/3/library ...
- Python之路(第四十六篇)多种方法实现python线程池(threadpool模块\multiprocessing.dummy模块\concurrent.futures模块)
一.线程池 很久(python2.6)之前python没有官方的线程池模块,只有第三方的threadpool模块, 之后再python2.6加入了multiprocessing.dummy 作为可以使 ...
- pyhton2 and python3 生成随机数字、字母、符号字典(用于撞库测试/验证码等)
本文介绍Python3中String模块ascii_letters和digits方法,其中ascii_letters是生成所有字母,从a-z和A-Z,digits是生成所有数字0-9.string.p ...
- Python3并发写文件
使用python2在进行并发写的时候,发现文件会乱掉,就是某一行中间会插入其他行的内容. 但是在使用python3进行并发写的时候,无论是多进程,还是多线程,都没有出现这个问题,难道是python3的 ...
- python3下multiprocessing、threading和gevent性能对比----暨进程池、线程池和协程池性能对比
python3下multiprocessing.threading和gevent性能对比----暨进程池.线程池和协程池性能对比 标签: python3 / 线程池 / multiprocessi ...
随机推荐
- sublime3中如何快速生成html头部信息
前提要安装Emmet 插件:已经结束了啊,不要以为下面的操作跟问题有关,下面是具体的生成头部信息方法 输入下边加粗的缩写,然后Tab,就可以了: 生成html4.01 Transitional用 ht ...
- 移动端h5实现拍照上传图片并预览&webuploader
.移动端实现图片上传并预览,用到h5的input的file属性及filereader对象:经测除了android上不支持多图片上传,其他基本ok实用: 一:先说一下单张图片上传(先上代码): html ...
- xcrun: error: invalid active developer path (/Applications/Xcode.app/Contents/Developer)解决办法
背景 mac下卸载了xcode,使用git等命令时就提示错误.invalid active path(Applications/Xcode.app/Contents/Developer),这种情况可以 ...
- Eclipse Ctrl + H 搜索文件不覆盖已打开文件解决办法
1.windows------->preferences
- 第一章:了解SQL_数据库基础
什么是数据库(database): 数据库(database)是保存有组织的数据的容器(通常是一个文件或一组文件).数据库是一种以某种有组织的方式存储的数据集合. 表(table): 表(tabl ...
- 《SQL Server 2008从入门到精通》--20180629
约束 主关键字约束(Primary Key Constraint) 用来指定表中的一列或几列组合的值在表中具有唯一性.建立主键的目的是让外键来引用. Primary Key的创建方式 在创建表时创建P ...
- js 时间日期函数小结
Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month &quo ...
- amazon interview
I'll be sitting for an Amazon interview in 3 months. Which website should I use to practice: SPOJ, H ...
- controller断点进入失败:包路径问题
controller 接受前端参数的方法(前端要有传值给controller的方法,后台要有接收值得方法) 1.@RequestParam 接收表单参数 2.@RequestBody 接收json字符 ...
- CSS学习摘要-语法和选择器
主要摘自网络开发者. 从最基本的层次来看,CSS是由两块内容组合而成的: 属性(Property):一些人类可理解的标识符,这些标识符指出你想修改哪一些样式,例如:字体,宽度,背景颜色等. 属性值(V ...