上线多线程版本

This commit is contained in:
JIAKUNHAO 2022-11-25 10:07:50 +08:00
parent fc265ad4f1
commit 9a4b11b054
2 changed files with 20 additions and 69 deletions

View File

@ -47,8 +47,8 @@ def start_train_algorithm():
def wrapped_function(): def wrapped_function():
param = request.args.get('param') param = request.args.get('param')
id = request.args.get('id') id = request.args.get('id')
# t = Thread(target=func, args=(param, id)) t = Thread(target=func, args=(param, id))
t = Process(target=func, args=(param, id), name=id) #t = Process(target=func, args=(param, id), name=id)
set_value(key=id, value=False) set_value(key=id, value=False)
t.start() t.start()
return output_wrapped(0, 'success', '成功') return output_wrapped(0, 'success', '成功')

View File

@ -9,101 +9,52 @@
""" """
import json import json
from typing import Union, List, Dict from typing import Union, List, Dict
import os
from app.core.common_utils import logger from app.core.common_utils import logger
from app.utils.JSONEncodeTools import MyEncoder from app.utils.JSONEncodeTools import MyEncoder
from configs.global_var import *
from app.utils.redis_config import redis_client
from multiprocessing import Process, Queue
import pickle
class WebsocketUtil: class WebsocketUtil:
def __init__(self): def __init__(self):
# self.active_connections = multiprocessing.Manager().list() self.active_connections: List = []
self.active_connections_dist = [] self.active_connections_dist: Dict = {}
def connect(self, ws, id: str): def connect(self, ws, id: str):
# 等待连接 # 等待连接
msg = ws.receive() msg = ws.receive()
# 存储ws连接对象 # 存储ws连接对象
if os.path.exists(f"{id}.pkl"): self.active_connections.append(ws)
wsFile = read(id=id) if id in self.active_connections_dist:
ws_list = wsFile.ws self.active_connections_dist[id].append(ws)
ws_list.append(ws)
data = WsFile(id=id, ws=ws_list)
write(id=id, ws=data)
else: else:
ws_list = [ws, ] ws_list = [ws, ]
data = WsFile(id=id, ws=ws_list) self.active_connections_dist[id] = ws_list
write(id=id, ws=data)
print("--;;-----------", ws_list)
def disconnect(self, ws, id): def disconnect(self, ws, id):
# 删除连接 # ws关闭时 移除ws对象
if os.path.exists(f"{id}.pkl"): if ws.closed:
os.remove(f"{id}.pkl") if ws in self.active_connections_dist.values():
self.active_connections.remove(ws)
self.active_connections_dist[id].pop(ws)
@staticmethod @staticmethod
async def send_personal_message(message: str, ws): async def send_personal_message(message: str, ws):
# 发送个人消息 # 发送个人消息
await ws.send(message) await ws.send(message)
# def broadcast(self, message: str): def broadcast(self, message: str):
# # 广播消息 # 广播消息
# # global active_connections for connection in self.active_connections:
# active_connections = redis_client.get_redis().get("active_connections") connection.send(message)
# if active_connections is not None:
# active_connections = json.loads(active_connections)
# for connection in active_connections:
# connection.send(message)
def send_message_proj_json(self, message: Union[str, int, List, Dict], id: str): def send_message_proj_json(self, message: Union[str, int, List, Dict], id: str):
# 广播该项目的消息 # 广播该项目的消息
active_connections = read(id=id) for connection in self.active_connections_dist[id]:
print("===================", type(active_connections.id), active_connections.id)
print("===================", type(active_connections.getWs()), active_connections.getWs()[0])
for connection in active_connections.getWs():
try: try:
connection.send(json.dumps(message, cls=MyEncoder, indent=4), ) connection.send(json.dumps(message, cls=MyEncoder, indent=4), )
except Exception as e: except Exception as e:
logger.error("websocket异常断开{}", e) logger.error("websocket异常断开{}", e)
self.disconnect(ws=connection, id=id)
manager = WebsocketUtil() manager = WebsocketUtil()
def write(id: str, ws: List):
print(f"序列化对象{ws}")
with open(f"{id}.pkl", "wb") as f:
pickle.dump(ws, f)
def read(id: str):
with open(f"{id}.pkl", "rb") as f:
wss = pickle.load(f)
print(wss)
print(f"反序列化对象{wss}")
return wss
class WsFile:
def __init__(self, id: str, ws: List) -> None:
self.id = id
self.ws = ws
def __getstate__(self):
pickled = {"id": self.id}
return pickled
def getWs(self):
return self.ws
def __setstate(self, pickled_dict):
self.__init__(pickled_dict['id'])