-1

I am trying to put a QTimer inside a QThreadPool.

The reason is that I need to count real-time since the start of a process (recording video), but the actual code seems to be lagging a bit and the time counted is far from real-time.

However, when trying to create a minimal example, I ran into a different problem: QTimer cannot run with a QThreadPool ! (There is already multiple threads going on in the actual code.)

So two questions:

1- How do You use QTimer with multiple threads, aka QThreadPool ?

2- Would that be the best way to get real-time from the start of an "heavy" process ?

Here is my test code:

from PyQt5.QtCore import QTimer, QThreadPool


class Test():
    def __init__(self):
        self.timerthread()
        print("Initialised")
        # self.Timing()

    def Timing(self):
        self.Timer = QTimer()
        self.Timer.moveToThread(self.threadpooltimer)
        self.Timer.timeout.connect(self.UpdateTiming)
        self.Timer.start(1) #per millisecond
        self.msec = 0 % 1000
        print("Timing is set")
    
    def UpdateTiming(self):
        self.msec += 1
        self.msecd = self.msec % 1000
        self.sec = self.msecd // 1000
        self.secd = self.sec % 60
        self.min = (self.sec // 60)
        self.mind = self.min % 60
        self.hour = self.min // 60
        self.time = f'{self.hour:02}:{self.mind:02}:{self.secd:02}:{self.msecd:03}'
        print(self.time)
    
    def timerthread(self):
        self.threadpooltimer = QThreadPool()
        self.threadpooltimer.start(self.Timing)
        print("Threading")

Test()

Thank You for your time!

Ad Ep
  • 23
  • 7

1 Answers1

0

For now, I think I managed to do it like this:

import time

start_time = time.perf_counter()
while True:
    current_time =  time.perf_counter() - start_time
    timeoncams = "{:.3f}".format(current_time)
    print(timeoncams)

The time seems accurate!

Ad Ep
  • 23
  • 7