起因
今天在写东西的时候,用到了多线程。遇到了个问题:
子线程的异常,在父线程中无法捕获。
解决
问题代码
问题代码示例代码如下:
import threading
class SampleThread(threading.Thread):
def run(self):
raise Exception('An error occured here.')
def main():
try:
thread_obj = SampleThread()
thread_obj.start()
except Exception:
print 'catch that'
if __name__ == '__main__':
main()
运行结果如下:
Exception in thread Thread-1:
Traceback (most recent call last):
File "E:\installDevelopDirectory\py27\lib\threading.py", line 801, in __bootstrap_inner
self.run()
File "E:/program/ljt_test/test/test_process.py", line 51, in run
raise Exception('An error occured here.')
Exception: An error occured here.
解决办法
通过查看资料:http://stackoverflow.com/questions/2829329/catch-a-threads-exception-in-the-caller-thread-in-python
出现上述问题是因为:执行到 thread_obj.start() 时,父线程就会立即返回结果。然后,生成的子线程在自己独立的上下文中执行,并使用自己的堆栈。子线程发生的任何异常都是在子线程的上下文中,并且它在它自己的堆栈中(独立于父线程)。
所以,解决办法是:将这些信息传递给父线程。代码如下:
#coding=utf-8
import threading
import Queue
import sys
class ExcThread(threading.Thread):
def __init__(self, bucket):
super(ExcThread, self).__init__()
self.bucket = bucket
def run(self):
try:
raise Exception('An error occured here.')
# pass
except Exception:
# 异常信息元祖放入队列传递给父进程
self.bucket.put(sys.exc_info())
def main():
bucket = Queue.Queue()
thread_obj = ExcThread(bucket)
thread_obj.start()
# 循环获取子线程的异常信息
while 1:
try:
exc = bucket.get(block=False)
except Queue.Empty:#系统已经定义了,可以直接用 pass
else:
exc_type, exc_obj, exc_trace = exc
# deal with the exception
print exc_type, exc_obj
print exc_trace
# 防止阻塞
thread_obj.join(0.1)
if thread_obj.isAlive():
continue
else:
break
if __name__ == '__main__':
main()
Queue文件中定义的:
https://www.cnblogs.com/xueweihan/p/6653610.html
https://www.cnblogs.com/wt11/p/5952500.html |