linjmeyer / tilt-pitch

Simple replacement for the Tilt Hydrometer mobile apps and TiltPi with lots of features
GNU General Public License v3.0
51 stars 16 forks source link

busy loop causes performance hit #23

Closed mschilt closed 3 years ago

mschilt commented 3 years ago

Hey mate

thank you for creating this script. It really helps me to get data from my tilts into our logging system. While playing around I noticed that this code uses a lot of CPU time (it maxes out a single core tbh).

To me it look like you created ssome kind of busy loop inside the scanner function. To fix this please add a time.sleep(1) inside the endless loop below this line: https://github.com/linjmeyer/tilt-pitch/blob/cece451ab744476b7ead1fb9e394c2f1c56c8645/pitch/pitch.py#L88 This causes the

Works like a charm and now uses ~1%CPU time.

Cheers -Manuel

P.S: oh and while we're on it .. my tilt ceates some kind of bogus enries at the start of a 'burst' where the temperature fahrenheit is always 999.. would be greate to filter them out

linjmeyer commented 3 years ago

Cool good catch! I run this on a dedicated Raspberry Pi, to be honest I never checked the resource usage.

Was able to replicate it easily. I implemented your suggested solution with one change: Pitch will only sleep when the queue is empty. This should ensure Pitch doesn't fall behind the Tilt's updates while still reducing CPU usage.

Feel free to give it a shot and let me know how it goes. Testing locally this reduced CPU usage from 49% to <1%. Version is v0.8.1

linjmeyer commented 3 years ago

I also released v0.8.3 which adds temperature/gravity ranges. When outside the ranges the broadcast is ignored (and logged). Details are in the config docs on the main page.