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 ...
随机推荐
- CSS相对定位|绝对定位(五)之z-index篇——张鑫旭
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1855 补充于2016 ...
- PHP报错Deprecated: Function ereg_replace() is deprecated in
可能用了PHP5.3乃至更高的PHP版本,目前DEDE中有很多地方的正则函数都用的ereg_replace,而这个函数现在在PHP5.3中已经被废止了. 解决办法: 如果一定要用php5.3,请修改p ...
- js二分查找树实现
function BinaryTree() { var Node = function(key) { this.key = key; this.left = null; this.right = nu ...
- for、for..in、forEach、$.each等循环性能测试
var num = 10000000,arr = []; for(i=0;i<num;i++){ arr[i] = i+2; } //1) 使用 for 循环 function test1() ...
- 使用tour_editor.html设置视角和添加热点
控制初始视角 双击打开vtour文件夹中的tour_editor.html.(请先运行测试服务器,然后在浏览器地址栏中加上tour_editor.html,例如 http://localhost:52 ...
- 日历视图(CalendarView)
日历视图(Calendarview) 常用属性: android:selectedWeekBackgroundColor(设置被选中周的背景颜色) android:showWeekNumber(设置是 ...
- ui-sref的参数传递
例如:路由配置如下: $stateProvider.state('admin.userList', { url: '/listUser?type&role', //参数必须先在这边声明 tem ...
- 在 Azure 中的 Linux 虚拟机上使用 SSL 证书保护 Web 服务器
若要保护 Web 服务器,可以使用安全套接字层 (SSL) 证书来加密 Web 流量. 这些 SSL 证书可存储在 Azure Key Vault 中,并可安全部署到 Azure 中的 Linux 虚 ...
- Windows Azure中WebSite 网站, Cloud Service 云服务,Virtual Machine 虚拟机的比较
在Windows Azure服务平台里,Web Site特点是: 在Windows Azure上构建高度可扩展的Web站点. 快速.轻松部署一个高度可扩展的云环境,并且可以从很小的规模开始. 使用您所 ...
- Vue2学习笔记:实例生命周期
实例生命周期 每个 Vue 实例在被创建之前都要经过一系列的初始化过程.例如,实例需要配置数据观测(data observer).编译模版.挂载实例到 DOM ,然后在数据变化时更新 DOM .在这个 ...