I am working on a PySide 6 GUI application that uses multi-threading. One of the threads is simply a 'clock' thread that should run once per second (and only once) to determine if certain signals should be emitted back to the main application. It is important for the signals to stay aligned with the system clock, so instead of setting the Qtimer to run every 1000 ms; singleShot is set to True and the timer triggers itself again by calculating the number of ms until the next whole second. This logic seems to be sound, but I have confirmed this thread is emitting per-second signals sometimes as often as 270x/minute!
class clockWorker(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self.tick_count = 0
self.running = False
def start(self):
self.timer = QTimer(self)
self.timer.setSingleShot(True)
self.timer.timeout.connect(self.on_tick)
self.running = True
self._on_tick()
def _on_tick(self):
if self.running:
now = datetime.now(timezone.utc)
wait_ms = (1_000_000 - now.microsecond) // 1000
self.timer.start(wait_ms)
self.tick_count += 1
if now.second == 0:
print("ticks in last minute: ", self.tick_count)
self.tick_count = 0
Can anyone else replicate this behavior, or explain to me why this code causes tick_count to increase beyond 60 (ticks per minute)?