Algorithm effect on the CPU - 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: Algorithm effect on the CPU (/thread-14819.html) |
Algorithm effect on the CPU - Faruk - Dec-19-2018 Hello guys. I have a python 2.7 script for my raspberry pi. My problem is When I run code, Raspberry pi use %20 of CPU. I think, wrong something in my code. I saw else program bigger than mine(Also it runs video and render image) but it use %5 of CPU. I think my algorithm is wrong but I couldn't find any solution. My code this. If you can helping to me, I will happy. Thanks your time. Program details is: Rpi connect with uart to stm. Stm transmit data to Rpi. Rpi read incoming data and it shows on the GUI. Also there is image on GUI. My python program runs for Rpi. #! /usr/bin/python # -*-coding: utf-8 -*- #-Library setting-# import RPi.GPIO as gpio import time import serial from Tkinter import * from ttk import Frame, Style, Button from PIL import ImageTk, Image import subprocess from playsound import playsound gpio.setmode(gpio.BCM) gpio.setwarnings(False) gpio.setup(33, gpio.IN) #-Buffer definition-# dataarray='' data='' uartData='' dstflg=False dspflg=False dataflg=False testflg=False floorinfoflg=False control_count=0 #-Serial port config-# try: stmSer=serial.Serial(port='/dev/ttyAMA0', baudrate=115200, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=0.00001 ) except serial.SerialException: import traceback traceback.print_exc() print "Stmser except error" L1=Label(root, text=(traceback.print_exc())) L1.place(x=10, y=400) #-GUI update according to data of 'CARSTAT'-# def updateGUI(data): global left_character global right_character global call_up global call_down global travel_up global travel_down global floorinfoflg global control try: left_character=(data[10:12]).decode("hex") right_character=(data[12:14]).decode("hex") petra_status_1=format(ord(data[14:16].decode("hex")),'08b') print "UpdateGUI" travel_down=petra_status_1[4] travel_up=petra_status_1[3] katno.config(text=left_character+right_character, font=("Arial", 300)) print "UpdaaeGUI work" if travel_up=="1": image=Image.open("/home/pi/Petra/upArrow2.png") photo=ImageTk.PhotoImage(image) label.config(image=photo) label.photo_ref=photo if left_character=="1" or left_character=="2": katno.config(text=left_character+right_character, font=("Arial", 210)) katno.place(x=0,y=40) else: katno.config(text=left_character+right_character, font=("Arial", 300)) katno.place(x=-90,y=-30) elif travel_down=="1": image=Image.open("/home/pi/Petra/downArrow2.png") photo=ImageTk.PhotoImage(image) label.config(image=photo) label.photo_ref=photo if left_character=="1" or left_character=="2": katno.config(text=left_character+right_character, font=("Arial", 210)) katno.place(x=0,y=40) else: katno.config(text=left_character+right_character, font=("Arial", 300)) katno.place(x=-90,y=-30) elif travel_up=="0" and travel_down=="0": image=Image.open("/home/pi/Petra/upArrow2.png") photo=ImageTk.PhotoImage(image) label.config(image='') label.photo_ref=photo if left_character=="0": katno.config(text=left_character+right_character, font=("Arial", 300)) katno.place(x=-150, y=-30) elif left_character=="1" or left_character=="2": katno.config(text=left_character+right_character, font=("Arial", 300)) katno.place(x=-150, y=-30) else: katno.config(text=left_character+right_character, font=("Arial", 300)) katno.place(x=-190, y=-30) else: print "travel conditions ERROR" root.after(15,loop) except: import traceback traceback.print_exc() print "UpdateGUI Error" prgloop() L2=Label(root, text=(traceback.print_exc())) L2.place(x=10, y=400) ###-Data Read -# def readuart(): global dataread global datalist global dataarray global dstflg global dspflg global testflg global stmSer while True: try: if stmSer.inWaiting()>0: dataread=stmSer.read(1) if testflg==True: dstflg=True dspflg=True if dataread=='@' and dstflg==False: dstflg=True dspflg=False dataarray='' elif dataread=='$' and dspflg==False: dspflg=True dstflg=False #print "dataarray: "+dataarray return dataarray dataarray='' else: if dstflg==True and dspflg==False: dataarray=dataarray+''.join(dataread) elif dstflg==True and dspflg==True: print "Reset loop" dataarray='' uartData='' writeda='' testflg=False dstflg=False dspflg=False root.after(5,readuart) elif dstflg==False and dspflg==False: print "Reset loop" dataarray='' uartData='' writeda='' testflg=False dstflg=False dspflg=False root.after(5,readuart) else: print "ERROR!!" input_value = gpio.input(33) ##Button Pgm_DW if input_value == True: testflg=True dataarray='' uartData='' writeda='' #print('The button has been pressed...') #else: #print "Data bekleniyor." except: import traceback traceback.print_exc() print "readUart Error" L3=Label(root, text=(traceback.print_exc())) L3.place(x=10, y=400) print "readUart Error" def prgloop(): print "Reset prgloop" testflg=False root.after(5,loop) def render(): image2=Image.open('/home/pi/Petra/image_new.jpg') photo2=ImageTk.PhotoImage(image2) label2=Label(image=photo2, bg="#142F5E") label2.image=photo2 label2.config(height=375, width=480) label2.place(x=0, y=430) #-Furcation to update function -# def loop(): global dataread global datalist global dataarray global dstflg global uartData global dspflg global data dspflg=False try: uartData=readuart() if uartData[:8]=='CARSTAT,': updateGUI(uartData) #print "data: "+uartData else: #print "İnvalid Data" root.after(5,loop) except: print "Update ERROR" import traceback traceback.print_exc() L4=Label(root, text=(traceback.print_exc())) L4.place(x=10, y=400) prgloop() #-Graphical Design-# try: root=Tk() root.title('V0.8') root.overrideredirect(0) katno=Label(root, text='1', font=("Arial", 300), width=2, fg='white', bg='#2080FF') katno.place(x=-90, y=-40) katno.config(padx=170) image=Image.open('/home/pi/Petra/upArrow2.png') photo=ImageTk.PhotoImage(image) label=Label(image=photo, bg="#2080FF") label.image=photo label.place(x=30, y=50) root.configure(background="#2080FF") root.geometry('480x400+0+0') render() loop() root.mainloop() except: import traceback traceback.print_exc() L5=Label(root, text=(traceback.print_exc())) L5.place(x=10, y=40) print "GUI ERROR" RE: Algorithm effect on the CPU - Larz60+ - Dec-19-2018 One thing that stands out is that your timeout is very short. Making it longer is OK, the port will not wait for timeout after data has been transferred, it's a maximum value. RE: Algorithm effect on the CPU - jeanMichelBain - Dec-19-2018 Hello, some thought I believe too things are imbricated in your program. 1 - It does loop and then after(5,loop), which stack again your loop into the system, probably increasing the stack, which is wrong. 2 - In "loop()", an "except: prgloop()" which does the same "after(5,loop)". If an exception occurs, it will stacked, and if a new one occurs, you will reach eventually the python exception stack limit. Not very important because you will be notified. 3 - If I'm good, "after(5,loop)" means you start the loop again after 5ms. It's very few, and it could be the reason of your cpu time waste. I don't know Tk, but in the GUI I know, I never do that because... it does use a lot of CPU for nothing. The purpose of a GUI system is to react after a GUI event, not to process some other system, like for instance a serial device. Anyway, you stack a new loop every 5ms, and this is wrong. You can get a proof of that if the free memory of your system decreases regularly (try htop utility on your RPI, in the standard repository). If it does not, I'm wrong, but I'm sure it cannot be a good practice. What you can do is : - change your code to make the GUI starting events as it wants and terminate the event processing as quickly as possible, and don't start something long at the end of this event process. - process the serial line in another thread, which will live alone and won't use too much the CPU. There are excellent threading modules coming with python ; choose one compatible with Tk. Does somebody has any hint about that ?. You can look and post in the GUI forum. - If you don't have any other solution, increase the 5ms (try for instance 20ms). You can also try the time module (ex: time.sleep(0.2)) which is good to give your system some breath. RE: Algorithm effect on the CPU - Faruk - Dec-19-2018 Thanks for your reply |