Exception thrown in multiprocessing Pool not detected

后端 未结 9 813
灰色年华
灰色年华 2020-11-29 18:44

It seems that when an exception is raised from a multiprocessing.Pool process, there is no stack trace or any other indication that it has failed. Example:

         


        
9条回答
  •  既然无缘
    2020-11-29 19:29

    I've had success logging exceptions with this decorator:

    import traceback, functools, multiprocessing
    
    def trace_unhandled_exceptions(func):
        @functools.wraps(func)
        def wrapped_func(*args, **kwargs):
            try:
                func(*args, **kwargs)
            except:
                print 'Exception in '+func.__name__
                traceback.print_exc()
        return wrapped_func
    

    with the code in the question, it's

    @trace_unhandled_exceptions
    def go():
        print(1)
        raise Exception()
        print(2)
    
    p = multiprocessing.Pool(1)
    
    p.apply_async(go)
    p.close()
    p.join()
    

    Simply decorate the function you pass to your process pool. The key to this working is @functools.wraps(func) otherwise multiprocessing throws a PicklingError.

    code above gives

    1
    Exception in go
    Traceback (most recent call last):
      File "", line 5, in wrapped_func
      File "", line 4, in go
    Exception
    

提交回复
热议问题