Python解决多进程间访问效率低的方法总结
作者:剑客阿良_ALiang 发布时间:2023-11-25 11:57:43
前言
最近在解决一些算法优化的问题,为了实时性要求,必须精益求精的将资源利用率用到极致。同时对算法中一些处理进行多线程或者多进程处理。
在对代码的调试过程中,发现在进程间队列使用耗时很长,特别是图片这种比较大的数据的时候。
可以先看一下我下面的demo是不是符合你的场景。
下面还有我的解决方案。
使用进程间Queue效率问题场景
代码样例如下,模拟从两个视频读取图片帧进行处理。
#!/user/bin/env python
# coding=utf-8
"""
@project : csdn-pro
@author : 剑客阿良_ALiang
@file : test13.py
@ide : PyCharm
@time : 2022-09-13 10:47:35
"""
import time
import cv2
from multiprocessing import Queue, Process
def fun1(q: Queue):
cap = cv2.VideoCapture("11.mp4")
a = []
while cap.isOpened():
ret, frame = cap.read()
if ret:
a.append(frame)
if len(a) == 25:
q.put(a)
a = []
time.sleep(0.038)
def fun2(q: Queue):
cap = cv2.VideoCapture("3333333.mp4")
a = []
while cap.isOpened():
ret, frame = cap.read()
if ret:
a.append(frame)
if len(a) == 25:
q.put(a)
a = []
time.sleep(0.038)
def fun3(q1: Queue, q2: Queue, q3: Queue):
while True:
st0 = time.time()
a1 = q1.get()
st1 = time.time()
a2 = q2.get()
st2 = time.time()
print("{} 耗时:{} - {}".format(time.time(), st1 - st0, st2 - st1))
q3.put((a1, a2))
def fun4(q3: Queue):
while True:
st0 = time.time()
a1, a2 = q3.get()
et = time.time()
print("hhhh耗时: {}".format(et - st0))
if __name__ == '__main__':
q1 = Queue()
q2 = Queue()
q3 = Queue()
p1 = Process(target=fun1, args=(q1,))
p2 = Process(target=fun2, args=(q2,))
p3 = Process(target=fun3, args=(q1, q2, q3,))
p4 = Process(target=fun4, args=(q3,))
p1.start()
p2.start()
p3.start()
p4.start()
p1.join()
p2.join()
p3.join()
p4.join()
代码说明:
1、上面模拟每秒25帧读取图片,并传递一个25帧的图片list给到队列。
我们看一下从queue获取图片list的效率。部分执行结果如下。
1663139091.3648114 耗时:1.6036181449890137 - 0.1361703872680664
hhhh耗时: 3.0635826587677
1663139093.056612 耗时:1.5302414894104004 - 0.1615591049194336
hhhh耗时: 1.6867034435272217
1663139094.7388775 耗时:1.5256507396697998 - 0.1566147804260254
hhhh耗时: 1.6849782466888428
1663139096.36547 耗时:1.4680161476135254 - 0.15857625007629395
hhhh耗时: 1.651228427886963
1663139097.9867501 耗时:1.4417593479156494 - 0.179520845413208
hhhh耗时: 1.609663963317871
1663139099.5894623 耗时:1.4391484260559082 - 0.16356372833251953
hhhh耗时: 1.7086796760559082
1663139101.3031366 耗时:1.5481102466583252 - 0.16556406021118164
hhhh耗时: 1.657604455947876
1663139102.9448056 耗时:1.470097303390503 - 0.1715717315673828
hhhh耗时: 1.5316739082336426
1663139104.5233243 耗时:1.4139580726623535 - 0.16456055641174316
Process finished with exit code -1
可以看出我们从进程队列get数据的耗时很长,从q3中同时获取的时间如蓝色标记,远大于1秒钟。
而整体获取图片帧的效率如红色标记,间隔时间大于1秒。
采用管道模式解决
这个时间间隔没法接受,我才用multiprocessing.Pipe管道来提前输入图片。
样例代码如下:
#!/user/bin/env python
# coding=utf-8
"""
@project : csdn-pro
@author : 剑客阿良_ALiang
@file : test13.py
@ide : PyCharm
@time : 2022-09-13 10:47:35
"""
import threading
import time
import cv2
from multiprocessing import Queue, Process, Pipe
def fun1(pipe_in):
cap = cv2.VideoCapture("11.mp4")
while cap.isOpened():
ret, frame = cap.read()
if ret:
ret, frame = cap.read()
pipe_in.send((int(time.time()), frame))
time.sleep(0.038)
def fun2(pipe_in):
cap = cv2.VideoCapture("3333333.mp4")
while cap.isOpened():
ret, frame = cap.read()
if ret:
ret, frame = cap.read()
pipe_in.send((int(time.time()), frame))
time.sleep(0.038)
def fun3(pipe_rev1, pipe_rev2):
def handle(pipe_rev1, q1):
_cul = 0
a = []
while True:
_t, _frame = pipe_rev1.recv()
if _cul == 0:
a.append(_frame)
_cul = _t
elif _t > _cul != 0:
if len(a) != 0:
q1.put(a)
_cul = _t
a = []
a.append(_frame)
elif _t == _cul != 0:
a.append(_frame)
q1 = Queue()
q2 = Queue()
threading.Thread(target=handle, args=(pipe_rev1, q1,)).start()
threading.Thread(target=handle, args=(pipe_rev2, q2,)).start()
while True:
if not q1.empty() and not q2.empty():
st0 = time.time()
_f1 = q1.get()
st1 = time.time()
_f2 = q2.get()
et = time.time()
print("{} 耗时:{} - {}".format(time.time(), st1 - st0, et - st1))
if __name__ == '__main__':
pipe_in1, pipe_out1 = Pipe()
pipe_in2, pipe_out2 = Pipe()
p1 = Process(target=fun1, args=(pipe_in1,))
p2 = Process(target=fun2, args=(pipe_in2,))
p3 = Process(target=fun3, args=(pipe_out1, pipe_out2,))
p1.start()
p2.start()
p3.start()
p1.join()
p2.join()
p3.join()
代码说明:
1、通过两个线程不停从管道接受并写到内存的Queue里面,提前放到当前进程内存里。
看一下间隔是否稳定,部分执行结果如下
1663139886.0722673 耗时:0.003930091857910156 - 0.005983591079711914
1663139887.6837587 耗时:0.09677457809448242 - 0.09172177314758301
1663139888.472634 耗时:0.061833858489990234 - 0.05984067916870117
1663139889.5441313 耗时:0.07132482528686523 - 0.07080578804016113
1663139890.548978 耗时:0.06183457374572754 - 0.06881546974182129
1663139891.5112402 耗时:0.0637204647064209 - 0.0718080997467041
1663139892.4756596 耗时:0.06682205200195312 - 0.06978344917297363
1663139893.5788367 耗时:0.06779074668884277 - 0.07928323745727539
时间间隔还是比较稳定的。
来源:https://blog.csdn.net/zhiweihongyan1/article/details/126851924


猜你喜欢
- 最好的学习方式就是实践。 我们通过导入gin包来深入学习。环境go 1.13.5goland 2019.3.1manjaro-gnome3.
- 如下所示:<select id="host_list" name="host_list" mu
- 表空间概述Oracle的表空间属于Oracle中的存储结构,是一种用于存储数据库对象(如:数据文件)的逻辑空间,是Oracle中信息存储的最
- 最近疫情比较严重,很多公司依靠阿里旗下的办公软件钉钉来进行远程办公,当然了,钉钉这个产品真的是让人一言难尽,要多难用有多难用,真的让人觉得阿
- 一、使用SQL Server全文搜索配置要使用SQL Server的全文搜索服务,需要进行如下配置。1、开启全文搜索服务:2、开启数据库的全
- 一、Go语言中Goroutine的基本原理Go语言里的并发指的是能让某个函数独立于其他函数运行的能力。Go语言的goroutine是一个独立
- 本文实例讲述了js中火星坐标、百度坐标、WGS84坐标转换实现方法。分享给大家供大家参考,具体如下://定义一些常量var x_PI = 3
- 1、csv简介CSV (Comma Separated Values) ,即逗号分隔值(也称字符分隔值,因为分隔符可以不是逗号),是一种常用
- 1.安装数据库1)yum -y install mysql-server(简单)yum命令自动从网上寻找mysql服务资源,下载至本地并完成
- 调用re库,通过使用compile、findall获取字符串中的emailimport reemail=re.compile(r
- 1. 简介有些时候在项目中,使用配置文件来配置一些灵活的参数是比较常见的事,因为这会使得代码的维护变得更方便。而ini配置文件是比较常用的一
- 正在看的ORACLE教程是:ORACLE常见错误代码的分析与解决三。 -----------------------------
- 文档 地址functools.partial作用:functools.partial 通过包装手法,允许我们 "重新定义"
- Cookie用于服务器实现会话,用户登录及相关功能时进行状态管理。要在用户浏览器上安装cookie,HTTP服务器向HTTP响应添加类似以下
- 问题描述:使用指令 python -m pip install --upgrade pip 升级pip时,Pycharm报错:Attribu
- 保存模型保存模型仅仅是为了测试的时候,只需要torch.save(model.state_dict, path)path 为保存的路径但是有
- 为什么,这么简单的一个python,我还要特意来写一篇文章呢?是因为留念下,在使用了Anaconda2和Anaconda3的基础上,现在需安
- 场景:主库DB:utf8字符集备库DB:gbk字符集需求:校验主备数据是否一致,并且修复校验过程:设置主库连接为utf8,设置备库连接为gb
- 图搜索之基于Python的迪杰斯特拉算法和弗洛伊德算法,供大家参考,具体内容如下Djstela算法#encoding=UTF-8MAX=9&
- 游戏玩法根据神庙逃亡,实现一个人躲避僵尸的小游戏,主要的是精灵、精灵组之间相撞、相交的处理。游戏开始随机出现一定的僵尸,随机移动,玩家在一位