Added exception handling

This commit is contained in:
= 2024-08-01 23:04:49 -04:00
parent 80fd16cbc6
commit 48c1fda83a
2 changed files with 48 additions and 32 deletions

View File

@ -82,6 +82,7 @@ if __name__ == "__main__":
last_battery_values = battery_queue.get() last_battery_values = battery_queue.get()
while True: while True:
try:
if not cpu_queue.empty(): if not cpu_queue.empty():
last_cpu_values = cpu_queue.get() last_cpu_values = cpu_queue.get()
if not memory_queue.empty(): if not memory_queue.empty():
@ -98,6 +99,8 @@ if __name__ == "__main__":
draw_battery(grid, last_battery_values[0], last_battery_values[1], foreground_value) draw_battery(grid, last_battery_values[0], last_battery_values[1], foreground_value)
draw_borders(grid, background_value) draw_borders(grid, background_value)
draw_to_LEDs(s, grid) draw_to_LEDs(s, grid)
except Exception as e:
print(f"Error in main loop: {e}")
time.sleep(0.05) time.sleep(0.05)

View File

@ -20,6 +20,7 @@ class DiskMonitorThread(threading.Thread):
def run(self): def run(self):
while True: while True:
try:
if not self.output_queue.full(): if not self.output_queue.full():
disk_io = psutil.disk_io_counters() disk_io = psutil.disk_io_counters()
read_usage = disk_io.read_bytes read_usage = disk_io.read_bytes
@ -43,7 +44,8 @@ class DiskMonitorThread(threading.Thread):
read_percent = min(1.0, read_rate / self.highest_read_rate) read_percent = min(1.0, read_rate / self.highest_read_rate)
write_percent = min(1.0, write_rate / self.highest_write_rate) write_percent = min(1.0, write_rate / self.highest_write_rate)
self.output_queue.put((read_percent, write_percent)) self.output_queue.put((read_percent, write_percent))
except Exception as e:
print(f"Error in DiskMonitorThread: {e}")
time.sleep(self.update_interval) time.sleep(self.update_interval)
class NetworkMonitorThread(threading.Thread): class NetworkMonitorThread(threading.Thread):
@ -61,6 +63,7 @@ class NetworkMonitorThread(threading.Thread):
def run(self): def run(self):
while True: while True:
try:
if not self.output_queue.full(): if not self.output_queue.full():
net_io = psutil.net_io_counters() net_io = psutil.net_io_counters()
sent_usage = net_io.bytes_sent sent_usage = net_io.bytes_sent
@ -84,7 +87,8 @@ class NetworkMonitorThread(threading.Thread):
sent_percent = min(1.0, sent_rate / self.highest_sent_rate) sent_percent = min(1.0, sent_rate / self.highest_sent_rate)
recv_percent = min(1.0, recv_rate / self.highest_recv_rate) recv_percent = min(1.0, recv_rate / self.highest_recv_rate)
self.output_queue.put((sent_percent, recv_percent)) self.output_queue.put((sent_percent, recv_percent))
except Exception as e:
print(f"Error in NetworkMonitorThread: {e}")
time.sleep(self.update_interval) time.sleep(self.update_interval)
class CPUMonitorThread(threading.Thread): class CPUMonitorThread(threading.Thread):
@ -100,6 +104,7 @@ class CPUMonitorThread(threading.Thread):
def run(self): def run(self):
while True: while True:
try:
if not self.output_queue.full(): if not self.output_queue.full():
cpu_usage = psutil.cpu_percent(percpu=True) cpu_usage = psutil.cpu_percent(percpu=True)
for i in range(self.cpu_count): for i in range(self.cpu_count):
@ -115,6 +120,8 @@ class CPUMonitorThread(threading.Thread):
if len(self.cpu_usage_history[0]) == self.max_history_size: if len(self.cpu_usage_history[0]) == self.max_history_size:
cpu_percentages = [sum(core_history) / self.max_history_size for core_history in self.cpu_usage_history] cpu_percentages = [sum(core_history) / self.max_history_size for core_history in self.cpu_usage_history]
self.output_queue.put(cpu_percentages) self.output_queue.put(cpu_percentages)
except Exception as e:
print(f"Error in CPUMonitorThread: {e}")
time.sleep(self.update_interval) time.sleep(self.update_interval)
class MemoryMonitorThread(threading.Thread): class MemoryMonitorThread(threading.Thread):
@ -129,6 +136,7 @@ class MemoryMonitorThread(threading.Thread):
def run(self): def run(self):
while True: while True:
try:
if not self.output_queue.full(): if not self.output_queue.full():
memory_usage = psutil.virtual_memory().percent / 100.0 memory_usage = psutil.virtual_memory().percent / 100.0
self.memory_usage_history.append(memory_usage) self.memory_usage_history.append(memory_usage)
@ -139,6 +147,8 @@ class MemoryMonitorThread(threading.Thread):
if len(self.memory_usage_history) == self.max_history_size: if len(self.memory_usage_history) == self.max_history_size:
avg_memory_usage = sum(self.memory_usage_history) / self.max_history_size avg_memory_usage = sum(self.memory_usage_history) / self.max_history_size
self.output_queue.put(avg_memory_usage) self.output_queue.put(avg_memory_usage)
except Exception as e:
print(f"Error in MemoryMonitorThread: {e}")
time.sleep(self.update_interval) time.sleep(self.update_interval)
class BatteryMonitorThread(threading.Thread): class BatteryMonitorThread(threading.Thread):
@ -150,12 +160,15 @@ class BatteryMonitorThread(threading.Thread):
def run(self): def run(self):
while True: while True:
try:
if not self.output_queue.full(): if not self.output_queue.full():
battery = psutil.sensors_battery() battery = psutil.sensors_battery()
if battery is not None: if battery is not None:
battery_percentage = battery.percent / 100.0 battery_percentage = battery.percent / 100.0
battery_plugged = battery.power_plugged battery_plugged = battery.power_plugged
self.output_queue.put((battery_percentage, battery_plugged)) self.output_queue.put((battery_percentage, battery_plugged))
except Exception as e:
print(f"Error in BatteryMonitorThread: {e}")
time.sleep(self.update_interval) time.sleep(self.update_interval)
if __name__ == "__main__": if __name__ == "__main__":