1. 程式人生 > >python except異常處理之後不退出,解決異常接著執行

python except異常處理之後不退出,解決異常接著執行

寫了個等待分析結果,解析分析結果json的指令碼
那個檔案生成時候有點大,有時候監測到新檔案就去解析可能檔案只生成了一半,就會丟擲異常退出當前執行緒,此次的分析結果就丟失了,如果load json檔案失敗,一般就是上百M到幾G的json大檔案,等待10秒,如果再次load失敗,重新再load一次,這樣指令碼看上去挺繁瑣的,監控執行緒又只能監控檔案的建立,修改和刪除,不知道建立的檔案是否寫完畢。

def run_analyze():
        sleep(2)
        berror = True
        temp = {}
        while berror == True:
            with open(self.filepath, 'r') as f:
            global filename,filescore,filesize,filebehavior,filestrings
                try:
                    temp = json.loads(f.read())
                    berror = False
                except:#KeyError, VauleError
                    print "analyze report is creating,please wait a moment..."
                    f.close()
                    sleep(5)
                    berror = True
                    pass
        filescore = float(temp['info']['score'])
        print ("filescore:%d" %filescore)