0

python で簡単なスクリプトを書きました。これは、IronMan Jarvis から着想を得た、しゃべるバッテリー モニターです。ここでの問題は、コードは完全に実行されますが、コードの実行中に RAM がいっぱいになり続けることです。最後に、RAM がいっぱいになると、PC がクラッシュします。何が問題なのですか?新しい変数は使用しません。polling で同じ変数を更新します。これは私のコードです-

# Get power status of the system using ctypes to call GetSystemPowerStatus

import ctypes
from ctypes import wintypes
import speech

def startmonitor():   
    class SYSTEM_POWER_STATUS(ctypes.Structure):
        _fields_ = [
                    ('ACLineStatus', wintypes.BYTE),
                    ('BatteryFlag', wintypes.BYTE),
                    ('BatteryLifePercent', wintypes.BYTE),
                    ('Reserved1', wintypes.BYTE),
                    ('BatteryLifeTime', wintypes.DWORD),
                    ('BatteryFullLifeTime', wintypes.DWORD),
                   ]
    SYSTEM_POWER_STATUS_P = ctypes.POINTER(SYSTEM_POWER_STATUS)
    GetSystemPowerStatus = ctypes.windll.kernel32.GetSystemPowerStatus
    GetSystemPowerStatus.argtypes = [SYSTEM_POWER_STATUS_P]
    GetSystemPowerStatus.restype = wintypes.BOOL

    status = SYSTEM_POWER_STATUS() #define an object of the class SYSTEM_POWER_STATUS
    if not GetSystemPowerStatus(ctypes.pointer(status)):
        raise ctypes.WinError()
    return status
x=0  #counting variable
def setflag0():
    for x in range(7):
        flag[x]=0
    return flag

def setxval():

    if(status.BatteryLifePercent==100):
        x=0
    elif(status.BatteryLifePercent<100 and status.BatteryLifePercent>30):
        x=1
    elif(status.BatteryLifePercent<=30 and status.BatteryLifePercent>15):
        x=2
    elif(status.BatteryLifePercent<=15 and status.BatteryLifePercent>5):
        x=3
    elif(status.BatteryLifePercent<=5):
        x=4
    return x
flag=[0,0,0,0,0,0,0]
speech.say("This is the talking battery monitor version 1.0")
while(1) :
    status=startmonitor()
    bat=setxval()
    if(bat!=0 and status.ACLineStatus==1 and flag[1]!=1):
        speech.say("All power systems being charged sir ! ")
        flag=setflag0()
        flag[1]=1
    elif(bat==0 and flag[6]!=1):
        if(status.ACLineStatus==1):
            speech.say("Battery : one hundred percent charged")
        elif(status.ACLineStatus==0):
            speech.say("Sir , You have full battery power")
        flag=setflag0()
        flag[6]=1
    elif(bat==1 and status.ACLineStatus==0 and flag[2]!=1):
        speech.say("Battery Level : ")
        speech.say(status.BatteryLifePercent)
        speech.say("percent")
        flag=setflag0()
        flag[2]=1
    elif(bat==2 and flag[3]!=1 and status.ACLineStatus==0):
        speech.say("Sir, i'm running low on battery . Please put me on charge !" )
        flag=setflag0()
        flag[3]=1
    elif(bat==3 and flag[4]!=1 and status.ACLineStatus==0):
        speech.say("Sir, you're now running on emergency backup power")
        flag=setflag0()
        flag[4]=1
    elif(bat==4 and flag[5]!=1 and status.ACLineStatus==0):
        speech.say(" Alert ! Power critical Sir, I might turn off in ")
        speech.say(status.BatteryLifeTime/1000000)
        speech.say("minutes")
        flag=setflag0()
        flag[5]=1
4

1 に答える 1