python 主线程捕获子线程异常

import threading
from _ast import Raise
from time import sleep

class ExcThread(threading.Thread):
    def __init__(self,group=None, target=None, name=None,
                 args=(), kwargs=None, verbose=None):
        threading.Thread.__init__(self, group, target, name, args, kwargs, verbose)
        if kwargs is None:
            kwargs = {}
        self.__target = target
        self.__args = args
        self.__kwargs = kwargs
        
    def run(self):
        self.exc = None
        try:
            # Possibly throws an exception
            if self.__target:
                self.__target(*self.__args, **self.__kwargs)
        except:
            import sys
            self.exc = sys.exc_info()
        finally:
            # Avoid a refcycle if the thread is running a function with
            # an argument that has a member that points to the thread.
            del self.__target, self.__args, self.__kwargs
    # Save details of the exception thrown but don't rethrow,
    # just complete the function
    
    def join(self):
        threading.Thread.join(self)
        if self.exc:
            msg = "Thread '%s' threw an exception: %s" % (self.getName(), self.exc[1])
            new_exc = Exception(msg)
            temp = self.exc[2]
            self.exc = None
            raise new_exc.__class__, new_exc, temp

def test1():
    for i in xrange(2):
        print i
    raise RuntimeError('success')
def test2():
    for i in xrange(5):
        print i
        sleep(1)

if __name__ == '__main__':
    th = ExcThread(target=test1)
    th2 = ExcThread(target=test2)
    while True:
        try:
            th.start()
            th2.start()
            th.join()
            th2.join()
        except Exception as e:
            print e
            th.join()
            th2.join()
            break
    print 'Done'

猜你喜欢

转载自blog.csdn.net/Walker_Xie/article/details/82527349
今日推荐