1

I write a .pyw file and run it on background. But, when I do nothing, it takes 29% CPU. If I start any other software, it starts to take 49% - 50% CPU. Is there any way to reduce the CPU usage?

Code:

def check():
    time = str(datetime.datetime.now())
    time = time[11:13] + time[14:16]
    today = str(datetime.datetime.today())
    today = today[:4] + today[5:7] + today[8:10]
    
    file = open("alarms\\Today\\today.txt", "r")
    alarms = file.readlines()
    file.close()
    
    ID = ""
    hasAlarm = False
    i = 0
    while i < len(alarms):
        if time in alarms[i]:
            hasAlarm = True
            ID = alarms[i+1]
            break
        i += 2
    
    if hasAlarm:
        file = open("alarms\\ID\\alarmID.txt", "r")
        alarms = file.readlines()
        file.close()
        i = 0
        while i < len(alarms):
            if ID in alarms[i]:
                break
            i += 6
        name = alarms[i+1][:-1]
        showtime = change24to12(alarms[i+2][:-1])
        if os.path.exists(alarms[i+5][:-1]):
            ringtone = alarms[i+5][:-1]
        else:
            ringtone = "basic.mp3"
        
        app = QApplication(sys.argv)
        window = AlarmWindow(name, showtime, ringtone)

        file = open("alarms\\Today\\today.txt", "r")
        alarms = file.readlines()
        file.close()
        file = open("alarms\\Today\\today.txt", "w")
        file.writelines(alarms[2:])
        file.close()
        
        file = open("alarms\\Special Days\\" + today + ".txt", "r")
        alarms = file.readlines()
        file.close()
        i = 0
        flag = False
        while i < len(alarms):
            if time in alarms[i]:
                flag = True
                break
            i += 2
        if flag:
            file = open("alarms\\Special Days\\" + today + ".txt", "w")
            file.writelines(alarms[:i] + alarms[i+2:])
            file.close()

        notification.notify(title=name, message=showtime)

        app.exec()
while True:
    check()

I want that the infinite loop will run and call the function from the background.

Debtanu Gupta
  • 65
  • 1
  • 6
  • We are unable to help you unless you provide a code sample – Kolay.Ne Nov 01 '20 at 11:58
  • @Kolay.Ne done! Now can you help me please? – Debtanu Gupta Nov 01 '20 at 12:30
  • Apart from my eyes now bleeding, you execute your code as fast as possible over and over again. Simplest solution, make it stop for a second: https://docs.python.org/3/library/time.html#time.sleep --- For opening files, use `with`: https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files -- Since you spawn a whole QtApp for a notification, you could just have everything execute in a QtApp and use Qt timers. – user136036 Nov 01 '20 at 12:36
  • @user136036 ha ha! :P But can you elaborate how using with instead of normal file open can save time? – Debtanu Gupta Nov 01 '20 at 12:40
  • @user136036 and how time.sleep(1) is reducing the CPU usage internally? – Debtanu Gupta Nov 01 '20 at 12:44
  • `with` is just best practice and saves a useless `close`. Well, when the program sleeps it uses no CPU... – user136036 Nov 01 '20 at 12:46
  • for 1 sec, a difference of 28-29% :O ?! – Debtanu Gupta Nov 01 '20 at 12:50

0 Answers0