python主线程捕获子线程的方法
作者:汤姆维赛迪 时间:2022-06-07 21:25:46
最近,在做一个项目时遇到的了一个问题,主线程无法捕获子线程中抛出的异常。
先看一个线程类的定义
'''''
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)
以上全部为本篇文章的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
来源:https://blog.csdn.net/linchere/article/details/49587479
标签:python,主线程,子线程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
嵌入式Web视频点播系统实现方法
2007-10-10 21:17:00
VB应用程序访问SQL Server的常用方法
2009-01-21 14:28:00
关于 Flash Banner 设计的建议
2010-02-02 18:19:00
SQL离前端有多远?
2009-05-21 10:37:00
JavaScript Table行定位效果
2009-05-25 10:47:00
python如何运行js语句
2022-04-10 02:34:27
![](https://img.aspxhome.com/file/2023/3/81603_0s.png)
asp如何用数据库制作一个多用户版的计数器?
2010-06-16 09:51:00
js序列化和反序列化的使用讲解
2023-08-05 08:13:24
IE8 的 noscript 标签 Bug
2009-08-18 12:37:00
![](https://img.aspxhome.com/file/UploadPic/20098/18/medium-28s.jpg)
VMware中linux环境下oracle安装图文教程(二)ORACLE 10.2.05版本的升级补丁安装
2023-07-22 11:37:11
![](https://img.aspxhome.com/file/2023/1/64091_0s.jpg)
php实现网站留言板功能
2023-11-23 21:06:36
![](https://img.aspxhome.com/file/2023/1/111361_0s.jpg)
教你用Python实现简易版学生信息管理系统(含源码)
2022-07-21 23:40:00
关于鼠标、键盘的几个事件的例子
2008-07-27 17:00:00
Python中实现远程调用(RPC、RMI)简单例子
2022-11-20 06:23:27
Ubuntu18.04中Python2.7与Python3.6环境切换
2021-10-13 17:21:02
![](https://img.aspxhome.com/file/2023/1/87901_0s.png)
python使用pyodbc连接sqlserver
2021-12-24 06:06:17
![](https://img.aspxhome.com/file/2023/1/69031_0s.png)
div、section、article 的区别
2011-02-26 15:39:00
python工具快速为音视频自动生成字幕(使用说明)
2021-04-14 15:15:26
python可变对象,不可变对象详解
2023-10-18 05:14:25
![](https://img.aspxhome.com/file/2023/5/76025_0s.png)
详解php实现页面静态化原理
2023-10-25 10:58:10