mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-03-15 12:24:29 +00:00
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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user