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 ...
随机推荐
- JSON 使用 教程
JSONP 教程 本章节我们将向大家介绍 JSONP 的知识. Jsonp(JSON with Padding) 是 json 的一种"使用模式",可以让网页从别的域名(网站)那获 ...
- python__new__与__init__的区别
__new__ __init__区别 1 class A(object): 2 def __init__(self,*args, **kwargs): 3 print "init A&quo ...
- Javaweb查询客户&分页部分代码
pageBean工具类代码(分页工具) package com.home.domain; import java.util.List; /** * 分页的JavaBean * @author Admi ...
- Solr常用操作命令
1. 新建collection ./solr create_collection -c collection_vip -d /opt/lucidworks-hdpsearch/solr/server/ ...
- 矩阵分解---QR正交分解,LU分解
相关概念: 正交矩阵:若一个方阵其行与列皆为正交的单位向量,则该矩阵为正交矩阵,且该矩阵的转置和其逆相等.两个向量正交的意思是两个向量的内积为 0 正定矩阵:如果对于所有的非零实系数向量x ,都有 x ...
- linux上设置mysql编码
linux下设置mysql编码 linux下设置mysql编码 首先查找MySql的cnf文件的位置: [root@flyHome gaoxiang]# find / -iname '*.cnf' - ...
- Oracle EBS 应收发票取值
SELECT ct.trx_number ,ctl.description ,fnd_flex_ext.get_segs('SQLGL' ,'GL#' ,gcc.chart_of_accounts_i ...
- C语言占位符
占位符就是先占住一个固定的位置,等着你再往里面添加内容的符号,广泛用于计算机中各类文档的编辑. 格式占位符(%)是在C/C++语言中格式输入函数,如 scanf.printf 等函数中使用.其意义就是 ...
- Matlab 之meshgrid, interp, griddata 用法和实例(转)
http://blog.sina.com.cn/s/blog_67f37e760101bu4e.html 实例结果http://wenku.baidu.com/link?url=SiGsFZIxuS1 ...
- (matlab)plot画图的颜色线型(转)
http://wenku.baidu.com/link?url=SVVMVH8QlDIu2hVKDtoBYs6l0CnQvFnFHJJ9yexmYVKQqhz47qIr7aK7LOf8nN0qNdy8 ...