Attempt to fix CPU utilisation bug by preventing the heartbeat callback leak in the SSE stream handlers and replacing Timer-based with Event-based threads. Also compiled regexes in advance for DXCC callsign lookups for efficiency, and fixed my misunderstanding of what Queue.empty() does

This commit is contained in:
Ian Renton
2026-02-27 08:28:43 +00:00
parent e6c9bb1853
commit 068c732796
19 changed files with 107 additions and 94 deletions

View File

@@ -1,6 +1,6 @@
import logging
from datetime import datetime
from threading import Timer
from threading import Timer, Event, Thread
from time import sleep
import pytz
@@ -18,17 +18,23 @@ class CleanupTimer:
self.cleanup_timer = None
self.last_cleanup_time = datetime.min.replace(tzinfo=pytz.UTC)
self.status = "Starting"
self._stop_event = Event()
# Start the cleanup timer
def start(self):
self.cleanup()
self._thread = Thread(target=self._run, daemon=True)
self._thread.start()
# Stop any threads and prepare for application shutdown
def stop(self):
self.cleanup_timer.cancel()
self._stop_event.set()
def _run(self):
while not self._stop_event.wait(timeout=self.cleanup_interval):
self._cleanup()
# Perform cleanup and reschedule next timer
def cleanup(self):
def _cleanup(self):
try:
# Perform cleanup via letting the data expire
self.spots.expire()
@@ -61,7 +67,4 @@ class CleanupTimer:
except Exception as e:
self.status = "Error"
logging.exception("Exception in Cleanup thread")
sleep(1)
self.cleanup_timer = Timer(self.cleanup_interval, self.cleanup)
self.cleanup_timer.start()
self._stop_event.wait(timeout=1)