![]() |
CPU usage with while Loop and LIRC [SOLVED] - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: CPU usage with while Loop and LIRC [SOLVED] (/thread-9437.html) |
CPU usage with while Loop and LIRC [SOLVED] - cygnus_X1 - Apr-08-2018 Is it normal to see 100% CPU usage with LIRC and a while loop? I have a Raspberry Pi , Model B (first version) single core CPU runs at 700MHz. I have installed music player daemon and LIRC. My code is simple but I was seeing 100% CPU usage (measured with top and htop). I I have found a solution to my problem now, and included a small 3ms time delay in the main loop. CPU useage is now only 7%, and as the remote control data rate is less than 36kHz the delay has no effect on program operation. Code attached: from time import sleep import i2clcd from i2clcd import * import subprocess import lirc lcd_init() tk="" def main(): sockid = lirc.init("myprog", blocking = False) #IR-code def decode(): track = subprocess.getoutput('mpc current') if track == "": return track = track.split(':', 1)[-1] # maxsplit 1st occurance only art = track.split('-')[0] tk = track.split(' -')[1:] tk = "".join(tk) # covert track from list to string print(art,tk) lcd_string1(art, line2) if len(str(tk)) > 20: lcd_string1(tk, line3) else: lcd_string1(tk, line3) menu = { 0:"Clear Display", 1:"Cinemix France", 2:"Smooth Jazz", 3:"Radio Rivendell", 4:"Costa del Mar", 5:"Slow Radio", 6:"Absolute 70's", 7:"Coast FM", 8:"Lazer Hot Hits", 9:"Radio 4 Extra", 10:"BBC World Service", } def playstream(n): lcd_cls() subprocess.call('mpc play ' +str(counter), shell=True) station = menu[int(counter)] lcd_string1(station) decode() while True: main() # Initialise LIRC codeIR = lirc.nextcode() #IR-code # Time.sleep reduces load average to 7%, 99% with no delay time.sleep(0.005) if codeIR == ['channelup'] or codeIR == ['up']: counter +=1 print(counter) lcd_string(str(counter) +" "+ menu[int(counter)]) if codeIR == ['channeldown'] or codeIR == ['down']: counter -=1 print(counter) lcd_string(str(counter) +" "+ menu[int(counter)]) if counter in range (1,11) and codeIR == ['ok']: subprocess.call('mpc play '+str(counter), shell=True) decode() if codeIR : print(codeIR) # debug prints IR code recd if codeIR == ['ok'] and counter == 0: lcd_cls() if codeIR in range (1,11): # Play stations 1-10 print(codeIR) lcd_string(codeIR,line1) if codeIR == ['setup']: n=(1,0)[n] # toggle backlight on/off lcd_bl(n) if codeIR == ['info']: subprocess.call('mpc current', shell=True) decode() if codeIR == ['stop']: subprocess.call('mpc stop', shell=True) lcd_cls() if codeIR == ['power']: subprocess.call('vcgencmd measure_temp', shell=True) temp = float(subprocess.check_output(["vcgencmd","measure_temp"])[5:9]) lcd_string("CPU temp " + str(temp) + "C", line3) if codeIR == ['volumedown']: subprocess.call('mpc volume -5', shell=True) if codeIR == ['volumeup']: subprocess.call('mpc volume +5', shell=True)There is probably a thousand different ways to achieve same result, but just wonder if high CPU usage was down to my bad programming skills, or possibly just low spec hardware? Line 52 fixed my 100% CPU useage RE: CPU usage with while Loop and LIRC - IAMK - Apr-08-2018 That's normal. If you don't have sleeps, your code will keep executing lines of code as quick as it can. RE: CPU usage with while Loop and LIRC - cygnus_X1 - Apr-08-2018 OK, thank you for quick reply. RE: CPU usage with while Loop and LIRC [SOLVED] - nilamo - Apr-09-2018 It's common to put a small sleep() in there for programs that are meant to run for very long periods of time, that way it doesn't eat the processor's time when it really doesn't need to. For example, at the end of your while loop, add a time.sleep(0.01) .
|