moved cpu usage to a grpc call that the gui will listen to, fixed grpc stream typing to be grpc.Future, fixed pygui issue for start callback when a start fails, but there are no exceptions

This commit is contained in:
Blake Harnden 2020-07-23 09:41:39 -07:00
parent 3544d00431
commit fff4bd7963
7 changed files with 85 additions and 47 deletions

View file

@ -1,10 +1,7 @@
"""
status bar
"""
import sched
import tkinter as tk
from pathlib import Path
from threading import Thread
from tkinter import ttk
from typing import TYPE_CHECKING, List, Optional
@ -16,41 +13,6 @@ if TYPE_CHECKING:
from core.gui.app import Application
class CpuUsage:
def __init__(self, statusbar: "StatusBar") -> None:
self.scheduler: sched.scheduler = sched.scheduler()
self.running: bool = False
self.thread: Optional[Thread] = None
self.prev_idle: int = 0
self.prev_total: int = 0
self.stat_file: Path = Path("/proc/stat")
self.statusbar: "StatusBar" = statusbar
def start(self) -> None:
self.running = True
self.thread = Thread(target=self._start, daemon=True)
self.thread.start()
def _start(self):
self.scheduler.enter(0, 0, self.run)
self.scheduler.run()
def run(self) -> None:
lines = self.stat_file.read_text().splitlines()[0]
values = [int(x) for x in lines.split()[1:]]
idle = sum(values[3:5])
non_idle = sum(values[:3] + values[5:8])
total = idle + non_idle
total_diff = total - self.prev_total
idle_diff = idle - self.prev_idle
cpu_percent = (total_diff - idle_diff) / total_diff
self.statusbar.after(0, self.statusbar.set_cpu, cpu_percent)
self.prev_idle = idle
self.prev_total = total
if self.running:
self.scheduler.enter(3, 0, self.run)
class StatusBar(ttk.Frame):
def __init__(self, master: tk.Widget, app: "Application") -> None:
super().__init__(master)
@ -64,8 +26,6 @@ class StatusBar(ttk.Frame):
self.running: bool = False
self.core_alarms: List[ExceptionEvent] = []
self.draw()
self.cpu_usage: CpuUsage = CpuUsage(self)
self.cpu_usage.start()
def draw(self) -> None:
self.columnconfigure(0, weight=7)