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



这是我编写的代码。它解析xml,并在某个条件为true时打开灯。我遇到的问题是,如果重新启动脚本,读取或处理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()

有人知道我的问题是从哪里来的吗?

感谢

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

真的,真的,确保你可以在try/except:的"继续"块中创建一个文件

with open('filepathfile.txt','w') as myfile:
    pass

然后,当你认为你已经杀死了脚本时,删除这个文件,看看它是否会回来。您必须将超时时间增加到1秒左右,以避免系统中充斥着文件创建请求。

不过,有两件事需要先尝试:

  • 去掉core()顶部的while True,似乎没有必要
  • 将你的呼叫循环频率降低到,比如说每秒一到两次

相关内容

  • 没有找到相关文章

最新更新