python主線程捕獲子線程的方法
最近,在做一個(gè)項(xiàng)目時(shí)遇到的了一個(gè)問題,主線程無法捕獲子線程中拋出的異常。
先看一個(gè)線程類的定義
''''' 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
很簡(jiǎn)單,傳入要調(diào)用的方法,并啟用一個(gè)新的線程來運(yùn)行這個(gè)方法。
在主線程中,啟動(dòng)這個(gè)線程類的一個(gè)對(duì)象時(shí),這要聲明一個(gè)對(duì)象然后啟動(dòng)就可以了,示例如下
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方法的棧因此無法捕獲到這一異常。
解決方法很簡(jiǎn)單,就是通過設(shè)置一個(gè)線程是否異常退出的flag的成員變量,當(dāng)線程異常退出時(shí),對(duì)其作一標(biāo)記。然后在主線程中檢查改線程運(yùn)行結(jié)束后該標(biāo)志位的值,如果異常,再通過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 # 如果線程異常退出,將該標(biāo)志位設(shè)置為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)
以上全部為本篇文章的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python多線程threading.Lock鎖用法實(shí)例
- Python多線程threading和multiprocessing模塊實(shí)例解析
- Python 多線程Threading初學(xué)教程
- Python多線程threading模塊用法實(shí)例分析
- Python多線程threading join和守護(hù)線程setDeamon原理詳解
- python 多線程中子線程和主線程相互通信方法
- python子線程退出及線程退出控制的代碼
- python從子線程中獲得返回值的方法
- python主線程與子線程的結(jié)束順序?qū)嵗馕?/a>
- Python 多線程,threading模塊,創(chuàng)建子線程的兩種方式示例
- python實(shí)現(xiàn)守護(hù)進(jìn)程、守護(hù)線程、守護(hù)非守護(hù)并行
- Python多線程Threading、子線程與守護(hù)線程實(shí)例詳解
相關(guān)文章
python使用OpenCV獲取高動(dòng)態(tài)范圍成像HDR
這篇文章主要介紹了python使用OpenCV獲取高動(dòng)態(tài)范圍成像HDR,如何使用不同曝光設(shè)置拍攝的多張圖像創(chuàng)建高動(dòng)態(tài)范圍圖像HDR,下文嗎更詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下2022-04-04python實(shí)現(xiàn)將Excel文件轉(zhuǎn)換為JSON文件
在數(shù)據(jù)處理和分析中,Excel和JSON是兩種常見的數(shù)據(jù)格式,本文將詳細(xì)介紹如何使用Python將Excel文件轉(zhuǎn)換為JSON文件,我們將使用pandas庫(kù),這是一個(gè)強(qiáng)大的數(shù)據(jù)分析工具,能夠方便地讀取和處理各種數(shù)據(jù)格式,需要的朋友可以參考下2024-07-07python socket發(fā)送TCP數(shù)據(jù)方式
這篇文章主要介紹了python socket發(fā)送TCP數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09PyTorch中l(wèi)oading fbgemm.dll異常的解決辦法
PyTorch是一個(gè)深度學(xué)習(xí)框架,當(dāng)我們?cè)诒镜卣{(diào)試大模型時(shí),可能會(huì)選用并安裝它,目前已更新至2.4版本,本文給大家介紹了PyTorch中l(wèi)oading fbgemm.dll異常的解決辦法,文中通過代碼和圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08python數(shù)據(jù)清洗中的時(shí)間格式化實(shí)現(xiàn)
本文主要介紹了python數(shù)據(jù)清洗中的時(shí)間格式化實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05python 讀取txt中每行數(shù)據(jù),并且保存到excel中的實(shí)例
下面小編就為大家分享一篇python 讀取txt中每行數(shù)據(jù),并且保存到excel中的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04