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 ...
随机推荐
- HTML标签参考(二)
一些重要的标签 • ol li <ol><li></li></ol>这是一组标签,它们二者都是成对出现的,每一个标签单独出现都是没有意义的事情. 这一 ...
- react生命周期获取异步数据
当react组件需要获取异步数据的时候,建议在ComponentDidMount周期里执行获取动作, 如果非异步数据,可以在ComponentWillMount获取 因为ComponentWillMo ...
- C# 新建文档CreateNewDocument
// Copyright 2010 ESRI// // All rights reserved under the copyright laws of the United States// and ...
- HBase Region分裂实现
分裂策略 不同的分裂策略的实现需要继承RegionSplitPolicy,主要实现两个方法: shouldSplit()表示是否需要分裂 getSplitPoint()得到分裂点rowkey 从 HB ...
- Oracle EBS 报错:此责任无可用函数。 更改责任或与您的系统管理员联系。
解决:修改配置文件 1.FND: Diagnostics (FND:诊断) 启用设为”是” 2.Utilities:Diagnostics (公用程序 ...
- vs2017 调试时出现 cannot connect to runtime process错误
用Visual Studio 2017 .net core进行开发时 ,调试运行项目时出现如下错误 解决方案,调试>选项,取消勾选,关闭对JavaScript的调试
- SweetAlert插件 弹框插件
sweetalert是一个漂亮的弹窗 中文网址: http://mishengqiang.com/sweetalert/ 它需要2个文件:sweetalert-dev.js和sweetalert.cs ...
- [C++] 用Xcode来写C++程序[4] 函数
用Xcode来写C++程序[4] 函数 此节包括引用函数,内联函数,防止修改函数入参,函数自身带有默认值. 引用函数:防止复制对象,减少系统开销 内联函数:编译的时候根据具体情形将代码嵌入进去,成不成 ...
- 使用Github的高级搜索功能
使用Github的高级搜索功能 1. 首先,提供Github高级搜索帮助页面 https://help.github.com/categories/search/ 2. 搜索语法 https://he ...
- Linux 系统的DNS配置文件
系统的DNS配置文件 方式一: 界面操作 setup -->界面配置网络,网关等 方式二: 修改配置文件 # 修改配置 ==>vi /etc/resolv.conf -->man r ...