最近,在做一個項目時遇到的了一個問題,主線程無法捕獲子線程中拋出的異常。
先看一個線程類的定義
''''' Created on Oct 27, 2015 @author: wujz ''' import threading class runScriptThread(threading.Thread): def __init__(self, funcName, *args): threading.Thread.__init__(self) self.args = args self.funcName = funcName def run(self): try: self.funcName(*(self.args)) except Exception as e: raise e
很簡單,傳入要調用的方法,并啟用一個新的線程來運行這個方法。
在主線程中,啟動這個線程類的一個對象時,這要聲明一個對象然后啟動就可以了,示例如下
import runScriptThread,traceback if __name__=='__main__': sth = 'hello world' try: aChildThread = runScriptThread(printSth, sth) aChildThread.start() aChildThread.join() except Exception as e: print(str(traceback.format_exc()))
但是這樣的代碼,main方法中無法捕獲子線程中的異常,原因在于start()方法將為子線程開辟一條新的棧,main方法的棧因此無法捕獲到這一異常。
解決方法很簡單,就是通過設置一個線程是否異常退出的flag的成員變量,當線程異常退出時,對其作一標記。然后在主線程中檢查改線程運行結束后該標志位的值,如果異常,再通過sys和traceback回溯異常信息,然后拋出即可。改寫后的異常類:
''''' Created on Oct 27, 2015 @author: wujz ''' import threading,traceback,sys class runScriptThread(threading.Thread): #The timer class is derived from the class threading.Thread def __init__(self, funcName, *args): threading.Thread.__init__(self) self.args = args self.funcName = funcName self.exitcode = 0 self.exception = None self.exc_traceback = '' def run(self): #Overwrite run() method, put what you want the thread do here try: self._run() except Exception as e: self.exitcode = 1 # 如果線程異常退出,將該標志位設置為1,正常退出為0 self.exception = e self.exc_traceback = ''.join(traceback.format_exception(*sys.exc_info())) #在改成員變量中記錄異常信息 def _run(self): try: self.funcName(*(self.args)) except Exception as e: raise e
改寫后的主線程:
import runScriptThread,traceback if __name__=='__main__': sth = 'hello world' try: aChildThread = runScriptThread(printSth, sth) aChildThread.start() aChildThread.join() except Exception as e: print(aChildThread.exc_traceback)
以上全部為本篇文章的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。
新聞熱點
疑難解答