python主線程捕獲子線程的方法
最近,在做一個項目時遇到的了一個問題,主線程無法捕獲子線程中拋出的異常。
先看一個線程類的定義
''''' 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
很簡單,傳入要調(diào)用的方法,并啟用一個新的線程來運行這個方法。
在主線程中,啟動這個線程類的一個對象時,這要聲明一個對象然后啟動就可以了,示例如下
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)
以上全部為本篇文章的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- python多線程threading.Lock鎖用法實例
- Python多線程threading和multiprocessing模塊實例解析
- Python 多線程Threading初學教程
- Python多線程threading模塊用法實例分析
- Python多線程threading join和守護線程setDeamon原理詳解
- python 多線程中子線程和主線程相互通信方法
- python子線程退出及線程退出控制的代碼
- python從子線程中獲得返回值的方法
- python主線程與子線程的結束順序實例解析
- Python 多線程,threading模塊,創(chuàng)建子線程的兩種方式示例
- python實現(xiàn)守護進程、守護線程、守護非守護并行
- Python多線程Threading、子線程與守護線程實例詳解
相關文章
Pandas分組聚合之使用自定義函數(shù)方法transform()、apply()
Pandas具有很多強大的功能,transform就是其中之一,利用它可以高效地匯總數(shù)據(jù)且不改變數(shù)據(jù)行數(shù),下面這篇文章主要給大家介紹了關于Pandas分組聚合之使用自定義函數(shù)方法transform()、apply()的相關資料,需要的朋友可以參考下2023-01-01
python如何通過正則匹配指定字符開頭與結束提取中間內(nèi)容
這篇文章主要介紹了python通過正則匹配指定字符開頭與結束提取中間內(nèi)容的操作方法,本文結合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-02-02
Python pandas入門系列之眾數(shù)和分位數(shù)
分位數(shù)(Quantile),也稱分位點,是指將一個隨機變量的概率分布范圍分為幾個等份的數(shù)值點,分析其數(shù)據(jù)變量的趨勢,而眾數(shù)(Mode)是代表數(shù)據(jù)的一般水平,這篇文章主要給大家介紹了Python pandas系列之眾數(shù)和分位數(shù)的相關資料,需要的朋友可以參考下2021-08-08
Python利用函數(shù)式編程實現(xiàn)優(yōu)化代碼
函數(shù)式編程(Functional Programming)是一種編程范式,它將計算視為函數(shù)的求值,并且避免使用可變狀態(tài)和循環(huán),在Python中還可以利用它的簡潔和高效來解決實際問題,下面我們就來學習一下它的具體用法吧2023-11-11
Python2.x利用commands模塊執(zhí)行Linux shell命令
這篇文章主要介紹了Python2.x利用commands模塊執(zhí)行Linux shell命令 的相關資料,需要的朋友可以參考下2016-03-03

