基本上,我试图制作一个在后台运行的Py脚本,以通知用户何时插入充电器和何时断开连接
我面临的问题是性能,脚本使用了更多的CPU时间(可能是因为我使用了while循环)
告诉我是否有更好的方法来处理这种情况。
我已经尝试使用睡眠时间,但这没用。
class Battery_code:
def __init__(self, c_var):
self.c_var = c_var
def mbox(self, title, text, style):
return Custom.windll.user32.MessageBoxW(0, text, title, style)
while 1:
battery = Reader.sensors_battery()
plugged = battery.power_plugged
percent = str(battery.percent)
if plugged is False and int(percent) in range(40, 80, 1) and c_var == 0:
speak.Speak("Charger is Disconnected Now")
c_var += 1
time.sleep(2)
if plugged is True and int(percent) in range(40, 80, 1) and c_var == 0:
speak.Speak("Charger is Connected Now")
c_var += 1
time.sleep(2)
if plugged is True and int(percent) > 80:
mbox('',
'Battery is at [' + percent + '%] and Still Plugged Please Unplug ', 0)
speak.Speak("Please Unplug the charger to increase battery life")
c_var = 0
if plugged is False and int(percent) < 40:
mbox('',
'Battery is at [' + percent + '%] Please Connect Charger ', 0)
speak.Speak("Please Connect charger to increase battery life")
c_var = 0
time.sleep(3)
答案 0 :(得分:0)
使用“ battery.secsleft”来计算用户还剩多少时间。
然后通过一些简单的数学运算,可以弄清楚何时发出警报并在此之前进入睡眠状态。 当然,这可能会因用法而异,因此您可以暂时使用一些额外的填充。
此外,在以下情况下: 如果插入为True并且int(percent)> 80: 和 如果插入为False并且int(percent)<40:
您忘记使用睡眠了。