我写了一个 .pyw 文件并在后台运行它。但是,当我什么都不做时,它会占用 29% 的 CPU。如果我启动任何其他软件,它开始占用 49% - 50% 的 CPU。有什么办法可以降低CPU使用率?
代码:
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()
我希望无限循环将运行并从后台调用该函数。