#πŸ”’ QTimers firing too often?

4 messages Β· Page 1 of 1 (latest)

drifting tusk
#

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)?

inland cypressBOT
#

@drifting tusk

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

inland cypressBOT
#

@drifting tusk

Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.