Python: Getting a traceback from a multiprocessing.Process

前端 未结 7 1412
后悔当初
后悔当初 2020-11-30 01:45

I am trying to get hold of a traceback object from a multiprocessing.Process. Unfortunately passing the exception info through a pipe does not work because traceback objects

7条回答
  •  醉话见心
    2020-11-30 02:15

    The same solutions as @Syrtis Major and @interfect but, tested with Python 3.6:

    import sys
    import traceback
    import functools
    
    def catch_remote_exceptions(wrapped_function):
        """ https://stackoverflow.com/questions/6126007/python-getting-a-traceback """
    
        @functools.wraps(wrapped_function)
        def new_function(*args, **kwargs):
            try:
                return wrapped_function(*args, **kwargs)
    
            except:
                raise Exception( "".join(traceback.format_exception(*sys.exc_info())) )
    
        return new_function
    

    Usage:

    class ProcessLocker(object):
        @catch_remote_exceptions
        def __init__(self):
            super().__init__()
    
        @catch_remote_exceptions
        def create_process_locks(self, total_processes):
            self.process_locks = []
            # ...
    

提交回复
热议问题