通过重新打开脚本,处理速度降低

时间:2015-11-26 06:37:43

标签: performance python-2.7 slowdown

这是我写的代码。 它解析xml并在某个条件为真时打开灯。 我遇到的问题是,如果我重新启动脚本,则需要更长时间才能读出或处理xml。如果我再次重启,则需要更长时间。所以在某个时刻需要10秒或更长的时间才能通过一个周期。

def core():

    import urllib                                                               #import urllib.request

    x = 0

    while True:

        ### XML Extraction ###
        from xml.dom import minidom

        xml = urllib.urlopen("http://192.168.60.242/xml")                       # xml = urllib.request.urlopen("http://192.168.60.242/xml")
        xml_string = xml.read()
        xml.close()

        re_string = xml_string[130:4000]
        re_string = re_string.replace('</TEXTAREA></FORM></BODY></HTML>', '')   #zwecks inkompatibilität mit Python 3.5, muss hier eine Änderung vorgenommen werden

        #parsing
        xmldoc = minidom.parseString(re_string)


        Sensor0Elm = xmldoc.getElementsByTagName('t0')                                  
        Sensor1Elm = xmldoc.getElementsByTagName('t1')
        #Sensor2Elm = xmldoc.getElementsByTagName('t2') 

        Sensor0Elm = Sensor0Elm[0]
        Sensor1Elm = Sensor1Elm[0]
        #Sensor2Elm = Sensor2Elm[0]

        Sensor0 = Sensor0Elm.childNodes[0].data
        Sensor1 = Sensor1Elm.childNodes[0].data
        #Sensor2 = Sensor2Elm.childNodes[0].data 


        Sensor0 = float(Sensor0)
        Sensor1 = float(Sensor1)
        #Sensor2 = float(Sensor2)


        #Datenaufbereitung
        print (Sensor0*100.000000000001)
        print (Sensor1*100.000000000001)
        #print (Sensor2*100)


        ### int to bin ###
        Sensor0=bin(int(Sensor0*100.000000000001))
        Sensor1=bin(int(Sensor1*100.000000000001))
        #Sensor2=bin(int(Sensor2*100))

        Sensor0 = Sensor0[2:]
        Sensor1 = Sensor1[2:]
        #Sensor2 = Sensor2[2:]

        Sensor0_count_int = int(len(str(Sensor0)))
        Sensor1_count_int = int(len(str(Sensor1)))
        #Sensor2_count_int = int(len(str(Sensor2)))

        Sub0 = int(8 - Sensor0_count_int)
        Sub1 = int(8 - Sensor1_count_int)
        #Sub2 = int(8 - Sensor2_count_int)

        Sensor0_compl = (str(Sub0*"0")+Sensor0)
        Sensor1_compl = (str(Sub1*"0")+Sensor1)
        #Sensor2_compl = (str(Sub2*"0")+Sensor2)

        x = x+1
        print (">>>", x ,"<<<")
        print (Sensor0_compl)
        print (Sensor1_compl)
        #print (Sensor2_compl)


        #############################
    #    import RPi.GPIO as GPIO
    #    GPIO.setmode(GPIO.BCM)

    #    GPIO.setup(4,GPIO.OUT)
    #    GPIO.setup(5,GPIO.OUT)
    #    GPIO.setup(6,GPIO.OUT)
    #    GPIO.setup(12,GPIO.OUT)
    #    GPIO.setup(13,GPIO.OUT)

    #    GPIO.setup(16,GPIO.OUT)
    #    GPIO.setup(17,GPIO.OUT)
    #    GPIO.setup(18,GPIO.OUT)
    #    GPIO.setup(19,GPIO.OUT)
    #    GPIO.setup(20,GPIO.OUT)

    #    GPIO.setup(21,GPIO.OUT)
    #    GPIO.setup(22,GPIO.OUT)
    #    GPIO.setup(23,GPIO.OUT)
    #    GPIO.setup(24,GPIO.OUT)
    #    GPIO.setup(25,GPIO.OUT)
        #############################

        #Sensor0
    #    AgCh3A=(Sensor0_compl[0:1])
    #    if AgCh3A=="0":
    #        GPIO.output(4,True)
    #    else:
    #        GPIO.output(4,False)

    #    AgPro2=(Sensor0_compl[1:2])
    #    if AgPro2=="0":
    #        GPIO.output(5,True)
    #    else:
    #        GPIO.output(5,False)

    #    CharRo440=(Sensor0_compl[2:3])
    #    if CharRo440=="0":
    #        GPIO.output(6,True)
    #    else:
    #        GPIO.output(6,False)

    #    AgInnoC=(Sensor0_compl[3:4])
    #    if AgInnoC=="0":
    #        GPIO.output(12,True)
    #    else:
    #        GPIO.output(12,False)

    #    AgInnoB=(Sensor0_compl[4:5])
    #    if AgInnoB=="0":
    #        GPIO.output(13,True)
    #    else:
    #        GPIO.output(13,False)

    #    ZK700=(Sensor0_compl[5:6])
    #    if ZK700=="0":
    #        GPIO.output(16,True)
    #    else:
    #        GPIO.output(16,False)

    #    AgF3000=(Sensor0_compl[6:7])
    #    if AgF3000=="0":
    #        GPIO.output(17,True)
    #    else:
    #        GPIO.output(17,False)

    #    ZK1200=(Sensor0_compl[7:8])
    #    if ZK1200=="0":
    #        GPIO.output(18,True)
    #    else:
    #        GPIO.output(18,False)

        #Sensor1
    #    AgProV3=(Sensor1_compl[3:4])
    #    if AgProV3=="0":
    #        GPIO.output(19,True)
    #    else:
    #        GPIO.output(19,False)

    #    MakWPG1=(Sensor1_compl[4:5])
    #    if MakWPG1=="0":
    #        GPIO.output(20,True)
    #    else:
    #        GPIO.output(20,False)

    #    AgExcell2eC=(Sensor1_compl[5:6])
    #    if AgExcell2eC=="0":
    #        GPIO.output(21,True)
    #    else:
    #        GPIO.output(21,False)

    #    AgCh2eC=(Sensor1_compl[6:7])
    #    if AgCh2eC=="0":
    #        GPIO.output(22,True)
    #    else:
    #        GPIO.output(22,False)

    #    AgCh3B=(Sensor1_compl[7:8])
    #    if AgCh3B=="0":
    #        GPIO.output(23,True)
    #    else:
    #        GPIO.output(23,False)
    #################################
    #    MM5=(Sensor1_compl[5:6])
    #    if MM5=="0":
    #        GPIO.output(24,True)
    #    else:
    #        GPIO.output(24,False)

    #    MM6=(Sensor1_compl[6:7])
    #    if MM6=="0":
    #        GPIO.output(25,True)
    #    else:
    #        GPIO.output(25,False)

    #    MM7=(Sensor2_compl[0:1])
    #    if MM7=="0":
    #        GPIO.output(1,True)
    #    else:
    #        GPIO.output(1,False)


        #Sensor2
    #    MMM0=int(Sensor2[2:3])
    #    if M0==1:
    #        GPIO.output(1,True)
    #    else:
    #        GPIO.output(1,False)

    #    MMM1=int(Sensor2[3:4])
    #    if M0==1:
    #        GPIO.output(1,True)
    #    else:
    #        GPIO.output(1,False)

    #   MMM2=int(Sensor2[4:5])
    #    if M0==1:
    #        GPIO.output(1,True)
    #    else:
    #        GPIO.output(1,False)

    #    MMM3=int(Sensor2[5:6])
    #    if M0==1:
    #        GPIO.output(1,True)
    #    else:
    #       GPIO.output(1,False)

    #    MMM4=int(Sensor2[6:7])
    #    if M0==1:
    #        GPIO.output(1,True)
    #    else:
    #        GPIO.output(1,False)

    #    MMM5=int(Sensor2[7:8])
    #    if M0==1:
    #        GPIO.output(1,True)
    #    else:
    #        GPIO.output(1,False)

    #    MMM6=int(Sensor2[8:9])
    #    if M0==1:
    #        GPIO.output(1,True)
    #    else:
    #        GPIO.output(1,False)

    #    MMM7=int(Sensor2[9:10])
    #    if M0==1:
    #        GPIO.output(1,True)
    #    else:
    #        GPIO.output(1,False)


        #import time
        #time.sleep(1)

        #GPIO.cleanup()



def main():
    import time
    while True:
        try:
            core()
        except:
            continue
        time.sleep(0.01)



main()

有人知道我的问题源自何处?

谢谢

1 个答案:

答案 0 :(得分:0)

我怀疑当你认为脚本没有关闭时。检查这一点的显而易见的方法是查看pythonw.exe是否仍在任务管理器中运行。

真的,真的,确定你可以在&#39;继续&#39;中创建一个文件。阻止try/except

with open('filepath\file.txt','w') as myfile:
    pass

然后当您认为您已杀死该脚本时,请删除此文件并查看其是否返回。您必须将超时时间增加到1秒左右,以避免系统因文件创建请求而泛滥。

首先尝试两件事:

  • 摆脱while True顶部的core(),似乎没必要
  • 降低您的通话频率,例如每秒一次或两次