Exception thrown in multiprocessing Pool not detected

后端 未结 9 830
灰色年华
灰色年华 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:45

    import logging
    from multiprocessing import Pool
    
    def proc_wrapper(func, *args, **kwargs):
        """Print exception because multiprocessing lib doesn't return them right."""
        try:
            return func(*args, **kwargs)
        except Exception as e:
            logging.exception(e)
            raise
    
    def go(x):
        print x
        raise Exception("foobar")
    
    p = Pool()
    p.apply_async(proc_wrapper, (go, 5))
    p.join()
    p.close()
    

提交回复
热议问题