QTimer expectations

Heads up! You've already completed this tutorial.

Mr_S_J_Childerley | 2021-01-14 20:45:35 UTC | #1

I have an PyQt5 script that reads values for sensors every three seconds. The system being monitored changes slowly so dropping a second here and there isn't a big deal.

However I am running on a mac (just upgraded to 11.1 ) which doesn't appear to-be very generous with the resources it gives to python. The Y axis in plot (below) is of the difference in time between samples, the X axis is table indexes not seconds. In same cases the time between QTimer timeouts is in excess of 40s. This despite the process running the PyQt5 script having almost 100% of a cpu. The longer the script runs the less responsive to user input it becomes

The GUI is just consists of 3 pygtgraphs


I experienced similar behaviour in earlier version macOS. Is this a ploy by Apple to get developers to write native applications?

martin | 2021-01-21 10:27:49 UTC | #2

Have you tried this on other platforms? I would expect inconsistent delays on most systems due to multitasking -- although is your Y axis seconds? A difference from 5 - 45 seconds seems a bit odd.

Something particular about QTimer is that the events are handled through the event queue, which means they can get delayed by other parts of your program. If you have Python functions that take a 5-10 seconds to run these can block the event queue, and so prevent any subsequent timers from firing.

Threads might suffer from similar problems (becaue of the GIL), would need to see the code to know for sure. You might want to consider running this time critical code in a separate process so it's completely isolated from the GUI, it should make quite a difference.

Mr_S_J_Childerley | 2021-01-21 21:26:07 UTC | #3

thanks Martin,

Yes the Y axis is time since the last timer fire.

I think in the first instance I'll use the timer to kick-off at thread to do all or some of the tasks currently performed by the timer method, however none of them are particularly heavy weight. Then if that bares no fruit I'll consider moving to a separate process.

Over 10,000 developers have bought Create GUI Applications with Python & Qt!
Create GUI Applications with Python & Qt6
Take a look

Downloadable ebook (PDF, ePub) & Complete Source code

Also available from Leanpub and Amazon Paperback

[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

QTimer expectations was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.