python 线程池实用总结
线程池的两张方法 submit 和map
from concurrent.futures import ThreadPoolExecutor
import time # def sayhello(a):
time.sleep(2)
return "hello: "+a def main():
seed = ["a","b","c"]
# 不使用线程
start1 = time.time()
for each in seed:
t1 = sayhello(each)
print(t1)
end1 = time.time()
print("time1: "+str(end1-start1))
print('------------------submit----------------------')
# 线程池submit用法
# 1.先把值存放在列表中
# 2.在遍历列表取返回值
# 3.将遍历获取的结果存放在列表中
start2 = time.time()
lst = []
result_lst = []
with ThreadPoolExecutor(3) as executor:
for each in seed:
t2 = executor.submit(sayhello, each)
lst.append(t2)
for i in lst:
print(i.result())
result_lst.append(i.result())
print(result_lst)
end2 = time.time()
print("time2: "+str(end2-start2)) print('---------------map----------------------')
# 线程池map的用法
# map的参数:迭代器
# map的返回值是:生成器
# 1、获取生成器(或直接使用list强制装换)
# 2、遍历取值
# 3、将遍历的结果放入新的列表
map_lst = []
start3 = time.time()
with ThreadPoolExecutor(3) as executor1:
t3 = executor1.map(sayhello, seed)
for t in t3:
map_lst.append(t)
print(map_lst)
end3 = time.time()
print("time3: "+str(end3-start3)) if __name__ == '__main__':
main()
other
from concurrent.futures import ThreadPoolExecutor
import time # def sayhello(a):
time.sleep(2)
foo = [1, a]
return foo def main():
seed = ["a","b","c"]
# 不使用线程
start1 = time.time()
for each in seed:
t1 = sayhello(each)
print(t1)
end1 = time.time()
print("time1: "+str(end1-start1)) print('---------------map----------------------')
# 线程池map的用法
# map 的结果是迭代器,使用for循环取值
map_lst = []
start3 = time.time()
with ThreadPoolExecutor(3) as executor1:
t3 = executor1.map(sayhello, seed)
for t in t3:
map_lst.extend(t)
print(map_lst)
end3 = time.time()
print("time3: "+str(end3-start3)) if __name__ == '__main__':
main()
区别
map:
1、提交的任务的函数是一样的
2、参数:只需要提交一次目标函数,目标函数的参数放在一个迭代器(列表,字典)
submit:
1、提交的任务函数是不一样的,或者执行的过程之可能出现异常(使用map执行过程中发现问题会直接抛出错误)
2、参数:submit每次都需要提交一个目标函数和对应的参数 结果:
map可以保证输出的顺序, submit输出的顺序是乱的
python 线程池实用总结的更多相关文章
- 自定义高级版python线程池
基于简单版创建类对象过多,现自定义高级版python线程池,代码如下 #高级线程池 import queue import threading import time StopEvent = obje ...
- 对Python线程池
本文对Python线程池进行详细说明介绍,IDE选择及编码的解决方案进行了一番详细的描述,实为Python初学者必读的Python学习经验心得. AD: 干货来了,不要等!WOT2015 北京站演讲P ...
- Python 线程池(小节)
Python 线程池(小节) from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor import os,time, ...
- python线程池ThreadPoolExecutor(上)(38)
在前面的文章中我们已经介绍了很多关于python线程相关的知识点,比如 线程互斥锁Lock / 线程事件Event / 线程条件变量Condition 等等,而今天给大家讲解的是 线程池ThreadP ...
- python线程池及其原理和使用
python线程池及其原理和使用 2019-05-29 17:05:20 whatday 阅读数 576 系统启动一个新线程的成本是比较高的,因为它涉及与操作系统的交互.在这种情形下,使用线程池可以很 ...
- python线程池示例
使用with方式创建线程池,任务执行完毕之后,会自动关闭资源 , 否则就需要手动关闭线程池资源 import threading, time from concurrent.futures impo ...
- Python线程池与进程池
Python线程池与进程池 前言 前面我们已经将线程并发编程与进程并行编程全部摸了个透,其实我第一次学习他们的时候感觉非常困难甚至是吃力.因为概念实在是太多了,各种锁,数据共享同步,各种方法等等让人十 ...
- python线程池实现
python 的线程池主要有threadpool,不过它并不是内置的库,每次使用都需要安装,而且使用起来也不是那么好用,所以自己写了一个线程池实现,每次需要使用直接import即可.其中还可以根据传入 ...
- 《转》python线程池
线程池的概念是什么? 在IBM文档库中这样的一段描写:“在面向对象编程中,创建和销毁对象是很费时间的,因为创建一个对象要获取内存资源或者其它更多资源.在Java中更是 如此,虚拟机将试图跟踪每一个对象 ...
随机推荐
- 浅谈LOG日志的写法
文章来源于公司的大牛 1 Log的用途 不管是使用何种编程语言,日志输出几乎无处不再.总结起来,日志大致有以下几种用途: l 问题追踪:通过日志不仅仅包括我们程序的一些bug,也可以在安装配置时,通 ...
- Character.digit()的意义
最近在阅读Integet.parseInt()源码时,遇到了Character.digit()这个方法,以前没有遇到过,更没使用过,这里查了资料就记录一下. 官方说法是: java.lang.Char ...
- ASP.NET MVC 实现页落网资源分享网站+充值管理+后台管理(16)之轻博客
源码下载地址:http://www.yealuo.com/Sccnn/Detail?KeyValue=c891ffae-7441-4afb-9a75-c5fe000e3d1c 项目到上面一步其实已经算 ...
- linux虚拟机设置固定IP并实现联网,主机与虚拟机实现互ping
ifconfig eth0 up 启用第一块网卡 onboot=yes 自动启动 service network restart 重启网络服务 使用虚拟机添加一块桥接网卡 cp eth0 eth1 复 ...
- 关于CPython中set集合的无序研究
set集合本身是无序的,但是无意间发现set集合中都是数字时set貌似有序了. 无论声明这个set时数字如何摆放,输出结果总是以一种固定的顺序!同样我将dict字典的key值设为int类型,这时候字典 ...
- H3CFTP操作示例
- Comb CodeForces - 46E (动态规划)
题面 Having endured all the hardships, Lara Croft finally found herself in a room with treasures. To h ...
- JS 手札记
addEventListener中的事件如果移除(removeEventListener)的话不能在事件中执行bind(this)否则会移除无效! // selectCurrent() // copy ...
- JQuery多个异步操作后执行(resolve,promise,when,done)
代码分享: //3秒后完成 function asyncThing1() { var dfd = $.Deferred(); setTimeout(function () { alert('async ...
- 使用Glide加载圆角图片
//设置图片圆角角度 RoundedCorners roundedCorners= new RoundedCorners(6); //通过RequestOptions扩展功能,override采样率, ...