技术标签: Python高阶知识 java 开发语言
concurrent.futures 模块提供异步执行可调用对象高层接口
而异步执行可以由 ThreadPoolExecutor 线程或由 ProcessPoolExecutor 使用单独的进程来实现。 两者都是实现抽像类 Executor 定义的接口。
一、ThreadPoolExecutor 线程池
线程池执行器:ThreadPoolExecutor类
- 快速导入:from concurrent.futures import ThreadPoolExecutor
如何使用:
- 创建一个线程池对象:ThreadPoolExecutor(max_workers=5)
参数:max_workers 表示最大可同时执行的线程数
- submit(任务函数)方法: 去提交任务执行
- shutdown()方法: 等待线程尺池中所有的任务执行完毕之后,在往下执行
- map(任务函数,参数)方法: 用于批量任务提交,前面参数是:任务函数 后面是任务函数参数
线程池的优点:
1.使用线程池可以避免频繁的去创建线程,销毁线程,可以减少内存的使用
ThreadPoolExecutor类基本语法使用:
# 创建一个队列对象
q = Queue()
# 生产数据
def work():
for i in range(5):
for a in range(20):
print(f"这是第{
i}轮数据插入,插入的数据为:{
a}")
q.put(a)
time.sleep(1)
# 创建一个线程池对象
# 参数:max_workers 表示最大可同时执行的线程数
tpool = ThreadPoolExecutor(max_workers=5)
# 1.submit(函数)方法:去提交任务
tpool.submit(work)
# 2.shutdown()方法:等待线程尺池中所有的任务执行完毕之后,在往下执行
tpool.shutdown()
print("---------------------- 结束执行 -----------------------")
ThreadPoolExecutor类上下文管理协议的语法使用:
上下文管理协议的语法:
with ThreadPoolExecutor(max_workers=5) as tpool:
tpool.submit(提交的任务函数)
使用上下文管理器后,无需使用shutdown()方法,在使用 with 语句时,会调用时将 wait 设为 True ,会一样等待
q = Queue()
# 生产数据
def work():
for i in range(5):
for a in range(20):
print(f"这是第{
i}轮数据插入,,插入的数据为:{
a}")
q.put(a)
time.sleep(1)
with ThreadPoolExecutor(max_workers=5) as tpool:
tpool.submit(work)
map方法(任务函数,参数): 用于批量任务提交
- map()方法去提交单个参数
def work(name):
for i in range(3):
print("========= 这是{}执行的第{}轮测试=========".format(name, i))
time.sleep(1)
with ThreadPoolExecutor(max_workers=5) as tpool:
# 1.map方法
tpool.map(work,[1,2,3,4,5])
# 与上面的map方法一样的效果
# 2.submit方法
for i in range(5):
tpool.submit(work)
print("---------------------- 结束执行 -----------------------")
- map()方法去提交多个参数
def work(name,age):
for i in range(3):
print("========= 这是{}执行的第{}轮测试,只有{}个=========".format(name,i,age))
time.sleep(1)
with ThreadPoolExecutor(max_workers=5) as tpool:
# 1.submit方法
tpool.submit(work,'henry',18)
# 2.map方法
tpool.map(work, ['henry', 'henry1'], [17, 18]) # 以数组的方式
print("---------------------- 结束执行 -----------------------")
二、ProcessPoolExecutor 进程池
进程池执行器:ProcessPoolExecutor类
- 快速导入:from concurrent.futures import ProcessPoolExecutor
如何使用:
- 创建一个进程池对象:创建一个进程池对象: ProcessPoolExecutor(max_workers=5)
参数:max_workers 表示最大可同时执行的进程数
- submit(任务函数)方法: 去提交任务执行
- shutdown()方法: 等待进程池中所有的任务执行完毕之后,在往下执行
- map(任务函数,参数)方法: 用于批量任务提交,前面参数是:任务函数 后面是任务函数参数
ProcessPoolExecutor类基本语法使用:
q = Queue()
def work():
for i in range(5):
for a in range(20):
print(f"这是第{
i}轮数据插入,,插入的数据为:{
a}")
q.put(a)
time.sleep(1) #
if __name__ == '__main__':
with ProcessPoolExecutor(max_workers=5) as tpool:
tpool.submit(work)
ProcessPoolExecutor类上下文管理协议的语法使用:
上下文管理协议的语法:
with ThreadPoolExecutor(max_workers=5) as tpool:
tpool.submit(提交的任务函数)
使用上下文管理器后,无需使用shutdown()方法,在使用 with 语句时,会调用时将 wait 设为 True ,会一样等待
q = Queue()
# 生产数据
def work():
for i in range(5):
for a in range(20):
print(f"这是第{
i}轮数据插入,,插入的数据为:{
a}")
q.put(a)
time.sleep(1)
with ProcessPoolExecutor(max_workers=5) as tpool:
tpool.submit(work)
进程池之间的数据通信问题:
- 同一个进程中多个线程之间使用: queue.Queue
import queue
q1 = queue.Queue()
- 多个进程之间数据通信的队列:multiprocessing.Queue
from multiprocessing import Queue
q1 = Queue()
- 进程池之间数据通信:multiprocessing.Manager().Queue
from multiprocessing import Manager
q2 = Manager().Queue
注意事项:
- 如果要使用Pool创建进程,就需要使用 multiprocessing.Manager()中的Queue(),而不是multiprocessing.Queue()
否则会得到一条如下的错误信息:RuntimeError: Queue objects should only be shared between processes through inheritance.
实例:
def work1(q):
for i in range(10):
q.put(i)
def work2(q):
for i in range(10):
print(q.get())
if __name__ == '__main__':
q2 = Manager().Queue()
with ProcessPoolExecutor(max_workers=2) as pool:
pool.submit(work1, q2)
pool.submit(work2, q2)
文章浏览阅读1.7w次,点赞2次,收藏27次。简单说明:本方案不是用来提速的,只是方便将 115 网盘的资源下载到群晖可以将 115 网盘的资源下载到任何地方,只是因为群晖是 7*24 小时开机的,所以更方便而已也可以将 Aria2 作为通用的下载器,而不仅局限于下载 115 网盘的资源安装 Aria2什么是 Aria2 ?aria2 是一个轻量级的多协议和多源命令行下载实用程序。它支持 HTTP/HTTPS、FTP、SFTP、 BitTorrent 和 Metalink 。aria2 可以最大程度利用你的网络带宽,你可._tailscale下载
文章浏览阅读150次。车辆工程毕业设计(文)-基于C语言的BP神经网络预测程序开发【全套设计】本科学生毕业设计系部名称:专业班级:学生姓名:指导教师:职 称:The Graduation Design for Bachelor's DegreeDevelopment on BP eural Network Prediction Program Based on C LanguageCandidate:Gao Xi..._车辆工程需要学的计算机语言
文章浏览阅读1.5k次,点赞2次,收藏13次。在上篇教程:OPC服务器软件Kepware Kepserver实现与Mysql数据库连接交互(一)中我们学习了MySQL数据库简介、OPC服务器软件Kepserver软件介绍、MySQL5.5数据库安装配置等内容。在OPC服务器软件Kepware Kepserver实现与Mysql数据库连接交互(二)Kepserver软件安装配置中我们学习了Kepserver软件安装配置等内容。目录五、MySQ..._kepserver写不进去值
文章浏览阅读1.8k次,点赞2次,收藏4次。Oracle数据库主机巡检之 HP_UNIX系统中 TOP/Vmstat/iostat/Glance 命令详解_hp unix top
文章浏览阅读391次。web351<?phperror_reporting(0);highlight_file(__FILE__);$url=$_POST['url'];$ch=curl_init($url);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);$result=curl_exec($ch);curl_close($ch);echo ($result);?>第一题,啥也没_ctfshow web357
文章浏览阅读575次。docker_运维进阶训练营百度云
文章浏览阅读464次。京东css页面如何制作京东css页面如何制作1,熟练操作前端代码就可以埃 2,做做多练就可以,遇到解决不了的难题即使去查找资料解决。学完html+css,做京东静态网页,为啥感觉那么难学完html+css,做京东静态网页,为啥感觉那么难???css不够熟练,其实css并没有想象那么简单,特别是用在响应式布局跟移动端的时候,各种需要自适应水平垂直居中,还要考虑兼容性。css有些属性还是有点难度的。多..._京东店pc端装修css
文章浏览阅读1.4k次。本文实现了AVL树,有几个注意点:insert和remove引起的失衡都可以用同样的旋转进行修复:Case1:k2的左子树的左子树比其右子树高2 —— k2进行左单旋Case4:k1的右子树的右子树比其左子树高2 —— k1进行右单旋Case2:k3的左子树的右子树比其右子树高2 —— k3进行左双旋 —— k3左子树先进行右单旋再对k3进行左单旋Case3:k1的右子树的左子树比其左子树高_实现动态顺序统计:avl树的数据结构,完成在动态集合中查找某个元素x在集合中的秩
文章浏览阅读162次。AtomicInteger源码分析Top:带着问题看源码在这周之前对原子类毫无了解,前面看了Unsafe类,对CAS操作有了一点了解。所以看AtomicInteger时的问题很简单,弄清楚它是什么类,有什么作用。参考:彤哥读源码https://mp.weixin.qq.com/s/DdwSC5bYgFCWwnb0jxkspg一:简介AtomicInteger是java并发包下面提供的..._atomicinteger这个比较简单,建议直接去看源码
文章浏览阅读1.1w次,点赞10次,收藏13次。问题:nltk.download() 时 遇到getaddrinfo failed解决方案:当NLTK下载程序启动时,其服务器索引更改为-http://nltk.org/nltk_data/可以通过进入NLTK Downloader窗口和File-> Change Server Index来更改此设置...._nltk download getaddinfo failed
文章浏览阅读6.4k次,点赞2次,收藏10次。hadoop安装后运行hadoop version 报找不到命令[root@localhost devlops]# hadoop versionERROR: JAVA_HOME is not set and could not be found.原因:需要配置环境变量,并且设置java_home的路径:1、编辑/etc/profileJAVA_HOME=/opt/devlops/jdk8export HADOOP_HOME=/opt/devlops/hadoopexport PATH=PATH_hadoop version
文章浏览阅读116次。sql语句类型:DDL:数据库定义语言DML:数据库操作语言 增删改DCL:数据控制语言DML:增加:insert into 表名(字段名) values (字段值);删除:delect from 表名 where 字段改:update 表名 set 字段=‘值’ where 字段插入:& 动态插入insert into emp(ename) values (&e..._dml与ocr