Skip to content

Commit 267658c

Browse files
committed
optimize performance
1 parent 1e81d66 commit 267658c

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ debug/*
1111
TODO.md
1212
test.py
1313
*.sh
14-
*.db
14+
*.db
15+
backup/*

app/view/main_window.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# coding: utf-8
22
from PyQt5.QtCore import QUrl
33
from PyQt5.QtGui import QIcon, QDesktopServices
4-
from PyQt5.QtWidgets import QApplication
4+
from qasync import QApplication
55

66
from qfluentwidgets import NavigationAvatarWidget, NavigationItemPosition, MessageBox, FluentWindow
77
from qfluentwidgets import FluentIcon as FIF

main.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
from app.common.RouterInfo import Router_HW
2-
# from PyQt5.QtGui import *
3-
# from PyQt5.QtWidgets import *
4-
51
import os
62
import sys
3+
import asyncio
74

85
from PyQt5.QtCore import Qt, QTranslator, QTimer, QTime
96
from PyQt5.QtGui import QFont, QIcon
10-
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QMenu, QAction
7+
from PyQt5.QtWidgets import QSystemTrayIcon, QMenu, QAction
118
from qfluentwidgets import FluentTranslator, DWMMenu
9+
from qasync import QEventLoop, QApplication
1210

1311
from app.common.config import cfg
1412
from app.view.main_window import MainWindow
1513
from app.common.global_logger import logger
14+
from app.common.RouterInfo import Router_HW
1615

1716

1817

@@ -53,7 +52,8 @@
5352
def show_window(button):
5453
if button == QSystemTrayIcon.Trigger:
5554
w.show()
56-
update()
55+
w.update_monitoring_status()
56+
# w.update_month_statistics()
5757
else:
5858
pass
5959

@@ -80,28 +80,38 @@ def show_window(button):
8080

8181
w.show()
8282

83-
def update():
83+
async def update():
8484
router.update_monitoring_status()
8585
icon = QIcon(router.get_battery_icon_path())
8686
tray.setIcon(icon)
8787
if w.isVisible():
8888
w.update_monitoring_status()
8989
w.update_month_statistics()
9090

91-
def update_traffic_statistics():
91+
async def update_traffic_statistics():
9292
if w.isVisible():
9393
router.update_traffic_statistics()
9494
w.update_traffic_statistics()
9595

96-
timer = QTimer()
97-
timer.timeout.connect(update)
98-
timer.start(15000)
96+
async def main():
97+
while True:
98+
await update()
99+
await asyncio.sleep(15) # Run update every 15 seconds
99100

100-
timer_traffic = QTimer()
101-
timer_traffic.timeout.connect(update_traffic_statistics)
102-
timer_traffic.start(1000)
103-
104-
app.exec_()
101+
async def traffic_statistics_main():
102+
while True:
103+
await update_traffic_statistics()
104+
await asyncio.sleep(1) # Run update_traffic_statistics every 1 second
105105

106-
106+
# Start the main and traffic statistics tasks
107+
loop = QEventLoop(app)
108+
asyncio.set_event_loop(loop)
109+
110+
loop.create_task(main())
111+
loop.create_task(traffic_statistics_main())
112+
113+
# Start the application event loop
114+
with loop:
115+
# loop.run_until_complete(app_close_event.wait())
116+
loop.run_forever()
107117

0 commit comments

Comments
 (0)