You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
3.7 KiB
77 lines
3.7 KiB
6 months ago
|
import multiprocessing
|
||
|
|
||
|
from threading import Lock
|
||
|
from core.ModelNode import ModelNode
|
||
|
from myutils.ConfigManager import myCongif
|
||
|
|
||
|
class ModelNodeManger:
|
||
|
def __init__(self,device_id,model_id,model_path,threshold=0.5,iou_thres=0.5):
|
||
|
self.ch_count = 0 #当前模型关联的通道数量
|
||
|
self.channel_list = {} #channel_id modelNodle
|
||
|
self.clist_Lock = Lock() # count的维护锁
|
||
|
self.modelNode_list = [] # model_Node
|
||
|
self.brun = multiprocessing.Value('b',True) #brun.value = False,brun.value = True
|
||
|
self.m_p_status = multiprocessing.Value('i',0)
|
||
|
self.model = None # 模型对象
|
||
|
#-------
|
||
|
self.max_count = myCongif.get_data('maxCount')
|
||
|
self.device_id = device_id
|
||
|
self.model_id = model_id
|
||
|
self.model_path = model_path
|
||
|
self.threshold = threshold
|
||
|
self.iou_thres = iou_thres
|
||
|
|
||
|
|
||
|
# #添加数据
|
||
|
# def pro_add_data(self,channel_id,data):
|
||
|
# pass
|
||
|
# # #加锁? -- 对于多线程这里的耗时会不会有点大。。。 model_nodel 反馈出去?????getModle_Nodel
|
||
|
# # with self.clist_Lock:
|
||
|
# # if channel_id in self.channel_list:
|
||
|
# # self.modelNode_list[channel_id].pro_add_data(data)
|
||
|
|
||
|
#暴露modelNodel -- 避免pro_add_data锁竞争
|
||
|
def getModle_Nodel(self,channel_id):
|
||
|
model_nodel = None
|
||
|
if channel_id in self.channel_list:
|
||
|
model_nodel = self.channel_list[channel_id]
|
||
|
return model_nodel
|
||
|
|
||
|
def start_model_th(self, channel_id, out_mq):
|
||
|
with self.clist_Lock:
|
||
|
if channel_id in self.channel_list: #已经在List 说明已经启动过了,但不应该重复调用
|
||
|
print("不应该重复调用start_model_th")
|
||
|
return True
|
||
|
else: # 还没入list ,查找可以插入的model_node
|
||
|
for model_node in self.modelNode_list:
|
||
|
if model_node.ch_count < self.max_count: # 没满可以插入
|
||
|
model_node.start_model_th(channel_id, out_mq) #+1
|
||
|
self.channel_list[channel_id] = model_node #新增 channel_di 与model_node的对应关系
|
||
|
return True
|
||
|
# 执行到这说明没有空的model_nodel -- 需要新建
|
||
|
model_node = ModelNode(self.device_id,self.model_path,self.max_count)
|
||
|
model_node.start_model_th(channel_id, out_mq) # 启动线程,并+1
|
||
|
#两个数据结构,新增节点
|
||
|
self.modelNode_list.append(model_node) #新增一个model_node节点
|
||
|
self.channel_list[channel_id] = model_node #新增 channel_di 与model_node的对应关系
|
||
|
#行动完更新数量
|
||
|
self.ch_count +=1
|
||
|
return True
|
||
|
|
||
|
def stop_model_th(self, channel_id):
|
||
|
with self.clist_Lock:
|
||
|
if channel_id in self.channel_list:
|
||
|
model_node = self.channel_list[channel_id]
|
||
|
model_node.stop_model_th(channel_id) #减-,根据情况停止工作进程、
|
||
|
# 需要考虑下model_node是否要删除,还是保留后重复利用-
|
||
|
if model_node.ch_count ==0:
|
||
|
self.modelNode_list.remove(model_node) #如何没有关联通道了,侧删除该modelnodel
|
||
|
del model_node #删除对象
|
||
|
del self.channel_list[channel_id] #删除channel_id 与 model_node的对应关心节点
|
||
|
#行动完更新数量
|
||
|
self.ch_count -= 1
|
||
|
return True
|
||
|
else:
|
||
|
print("不在list里面,不应该调用stop_model_th")
|
||
|
return False
|