Python Forum
CPU usage with while Loop and LIRC [SOLVED]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CPU usage with while Loop and LIRC [SOLVED]
#1
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
Reply
#2
That's normal. If you don't have sleeps, your code will keep executing lines of code as quick as it can.
Reply
#3
OK, thank you for quick reply.
Reply
#4
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).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,461 May-14-2023, 04:29 PM
Last Post: Winfried
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,140 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 931 Nov-16-2022, 07:58 PM
Last Post: Winfried
  [SOLVED] [BS] Why new tag only added at the end when defined outside the loop? Winfried 1 974 Sep-05-2022, 09:36 AM
Last Post: snippsat
  Loop through list of ip-addresses [SOLVED] AlphaInc 7 3,986 May-11-2022, 02:23 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020