优化执行器,防止崩溃!

This commit is contained in:
毛鹏
2024-04-29 16:03:23 +08:00
parent 6404879db0
commit b3d79d664e
8 changed files with 128 additions and 47 deletions

View File

@@ -1,12 +1,85 @@
list1 = [1, 2, 3, 4, 5]
list2 = ['a', 'b', ]
# encoding: utf-8
# 获取两个列表的最大长度
max_len = max(len(list1), len(list2))
from PySide6.QtGui import QAction, QIcon, QKeySequence, QShortcut
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QMenu, QSystemTrayIcon
for i in range(max_len):
# 从list1和list2中获取元素,如果索引超出列表长度,则从头开始
item1 = list1[i % len(list1)]
item2 = list2[i % len(list2)]
print(f"List1 item: {item1}, List2 item: {item2}")
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("最小化系统托盘")
self.resize(400, 300)
# 创建按钮并连接到最小化到系统托盘的方法
self.button = QPushButton("Minimize to Tray")
self.button.clicked.connect(self.minimize_to_tray)
self.setCentralWidget(self.button)
# 初始化系统托盘相关的对象和菜单项
self._restore_action = QAction()
self._quit_action = QAction()
self._tray_icon_menu = QMenu()
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(QIcon("trash.png")) # 替换为你的图标路径
self.tray_icon.setToolTip("My Application")
self.create_actions()
self.create_tray_icon()
self.tray_icon.show()
# 连接系统托盘图标的激活事件
self.tray_icon.activated.connect(self.tray_icon_activated)
# 应用程序键盘监听
self.listen_keyboard()
def minimize_to_tray(self):
# 最小化窗口到系统托盘
self.hide()
def restore_from_tray(self):
# 还原窗口
if self.isMinimized():
self.showNormal()
elif self.isMaximized():
self.showMaximized()
else:
self.show()
def tray_icon_activated(self, reason):
# 当系统托盘图标被点击时的处理
if reason == QSystemTrayIcon.ActivationReason.Trigger:
# 如果点击的是触发事件(比如左键单击),则还原窗口
self.restore_from_tray()
def create_actions(self):
# 创建菜单项
self._restore_action = QAction("显示", self)
self._restore_action.triggered.connect(self.restore_from_tray) # "显示"菜单项触发还原窗口的操作
self._quit_action = QAction("退出", self)
self._quit_action.triggered.connect(QApplication.quit) # "退出"菜单项触发退出应用程序的操作
def create_tray_icon(self):
# 创建系统托盘图标和上下文菜单
self._tray_icon_menu = QMenu(self)
self._tray_icon_menu.addAction(self._restore_action)
self._tray_icon_menu.addSeparator()
self._tray_icon_menu.addAction(self._quit_action)
self.tray_icon.setContextMenu(self._tray_icon_menu)
self.tray_icon.show()
def listen_keyboard(self):
# 键盘监听
shortcut = QShortcut(QKeySequence("Esc"), self)
# 当按下 Esc 键时隐藏窗口
shortcut.activated.connect(self.hide)
if __name__ == "__main__":
app = QApplication()
app.setQuitOnLastWindowClosed(False)
window = MyWindow()
window.show()
app.exec()

View File

@@ -4,8 +4,6 @@
# @Time : 2023-09-28 15:49
# @Author : 毛鹏
import os
from PySide6.QtCore import QThread
from PySide6.QtGui import QIcon, QAction
from PySide6.QtWidgets import QMainWindow, QApplication, QSystemTrayIcon, QMenu, QLabel
@@ -31,7 +29,6 @@ class WebSocketThread(QThread):
def stop(self):
if self.socket is not None:
self.socket.cancel_tasks()
os._exit(0)
self.quit()
self.wait()
@@ -44,7 +41,7 @@ class MainWindow(QMainWindow, Window):
# 创建系统托盘图标
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(QIcon(r"D:\GitCode\MangoActuator\scripts\微信头像.jpg")) # 设置托盘图标
self.tray_icon.setIcon(QIcon('"D:\测试文件\测试素材\header.jpg"')) # 设置托盘图标
# 创建托盘图标菜单
tray_menu = QMenu(self)
@@ -86,7 +83,8 @@ if __name__ == "__main__":
LoginModel(ip='127.0.0.1',
port='8000',
nickname='毛鹏',
username='18071710220',
username='17798339533',
password='123456',
user_id='1',
token='123')
app = QApplication([])

View File

@@ -17,7 +17,7 @@ pydantic==2.6.0
cachetools==5.3.1
diskcache==5.6.3
pyside6==6.5.2
pyside6==6.7.0
jsonpath==0.82
Faker==17.6.0
colorlog==6.7.0

View File

@@ -13,21 +13,26 @@ from service.socket_client.api_reflection.api_consumer import APIConsumer
from service.socket_client.api_reflection.perf_consumer import PerfConsumer
from service.socket_client.api_reflection.tools_consumer import ToolsConsumer
from service.socket_client.api_reflection.ui_consumer import UIConsumer
from tools.desktop.signal_send import SignalSend
from tools.log_collector import log
class InterfaceMethodReflection(UIConsumer, APIConsumer, PerfConsumer, ToolsConsumer):
def __init__(self):
# custom_signal.disconnect(self.consumer)
SignalSend.custom.connect(self.consumer)
self.queue = asyncio.Queue()
self.loop = asyncio.get_event_loop()
self.loop.create_task(self.consumer())
def consumer(self, sender, data: dict):
if sender == "break":
os._exit(0)
task = asyncio.create_task(getattr(self, sender)(data))
task.add_done_callback(self.handle_task_result)
async def consumer(self):
while True:
if not self.queue.empty():
data: QueueModel = await self.queue.get()
if data.func_name == "break":
os._exit(0)
task = self.loop.create_task(getattr(self, data.func_name)(data.func_args))
task.add_done_callback(self.handle_task_result)
else:
await asyncio.sleep(0.1)
@classmethod
def handle_task_result(cls, task):
@@ -39,15 +44,18 @@ class InterfaceMethodReflection(UIConsumer, APIConsumer, PerfConsumer, ToolsCons
log.error(f"反射任务执行出现异常:{e}")
async def test(self):
with open(r'..\..\..\test.json', 'r', encoding='utf-8') as f:
with open(r'test.json', 'r', encoding='utf-8') as f:
out = json.load(f)
for i in out:
data = QueueModel(**i)
await getattr(self, data.func_name)(data.func_args)
await getattr(self, 'u_case')(i)
while True:
await asyncio.sleep(1)
r = InterfaceMethodReflection()
if __name__ == '__main__':
asyncio.run(r.test())
# pass

View File

@@ -76,12 +76,14 @@ class ClientWebSocket:
接受消息
@return:
"""
from service.socket_client.api_reflection import r
while True:
try:
recv_json = await websocket.recv()
data = self.__output_method(recv_json)
if data.data:
SignalSend.func_signal(data.data.func_name, data=data.data.func_args)
await r.queue.put(data.data)
# SignalSend.func_signal(data.data.func_name, data=data.data.func_args)
await asyncio.sleep(5)
except websockets.ConnectionClosed:
SignalSend.notice_signal_a('已离线')

View File

@@ -8,13 +8,11 @@ import traceback
from PySide6.QtWidgets import QApplication
from desktop.login_window import LoginWindow
from service.socket_client.api_reflection import InterfaceMethodReflection
from tools import InitPath
from tools.log_collector import log
try:
InitPath()
InterfaceMethodReflection()
app = QApplication([])
window = LoginWindow()
window.show()

View File

@@ -91,24 +91,24 @@ def ad_routes():
},
],
},
{
"menuUrl": "/perf",
"menuName": "性能自动化",
"icon": "IconMinus",
"parentPath": "",
"children": [
{
"parentPath": "/perf",
"menuUrl": "/perf/prepare",
"menuName": "接口准备",
},
{
"parentPath": "/perf",
"menuUrl": "/perf/report",
"menuName": "测试报告",
},
],
},
# {
# "menuUrl": "/perf",
# "menuName": "性能自动化",
# "icon": "IconMinus",
# "parentPath": "",
# "children": [
# {
# "parentPath": "/perf",
# "menuUrl": "/perf/prepare",
# "menuName": "接口准备",
# },
# {
# "parentPath": "/perf",
# "menuUrl": "/perf/report",
# "menuName": "测试报告",
# },
# ],
# },
{
"menuUrl": "/equipment",
"menuName": "设备中心",

View File

@@ -85,6 +85,8 @@ class UiTestRun:
case_model = self.send_case(case_id, test_suite_id)
self.__socket_send(func_name=UiSocketEnum.CASE_BATCH.value,
case_model=case_model)
# with open(r'D:\GitCode\MangoTestingPlatform\MangoServer\test.json', 'w') as f:
# json.dump(case_model_list, f, indent=4, ensure_ascii=False)
def send_case(self, case_id: int, test_suite_id) -> CaseModel:
if self.tasks_id: