Turn TimerManager into a Singleton

This commit is contained in:
2025-05-20 20:18:25 +02:00
parent 69b6f6e860
commit 7712c25627
3 changed files with 17 additions and 12 deletions

View File

@@ -5,13 +5,20 @@ import asyncio
import heapq
import time
TIMER_DEBUG = True
class TimerManager:
def __init__(self, timer_debug=False):
self.timers = []
self.timer_debug = timer_debug
self.task = asyncio.create_task(self._timer_worker())
self.worker_event = asyncio.Event()
class TimerManager(object):
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(TimerManager, cls).__new__(cls)
cls._instance.timers = []
cls._instance.timer_debug = TIMER_DEBUG
cls._instance.task = asyncio.create_task(cls._instance._timer_worker())
cls._instance.worker_event = asyncio.Event()
return cls._instance
def schedule(self, when, what):
cur_nearest = self.timers[0][0] if len(self.timers) > 0 else None