Exception thrown in multiprocessing Pool not detected

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

    Since there are already decent answers for multiprocessing.Pool available, I will provide a solution using a different approach for completeness.

    For python >= 3.2 the following solution seems to be the simplest:

    from concurrent.futures import ProcessPoolExecutor, wait
    
    def go():
        print(1)
        raise Exception()
        print(2)
    
    
    futures = []
    with ProcessPoolExecutor() as p:
        for i in range(10):
            futures.append(p.submit(go))
    
    results = [f.result() for f in futures]
    

    Advantages:

    • very little code
    • raises an exception in the main process
    • provides a stack trace
    • no external dependencies

    For more info about the API please check out this

    Additionally, if you are submitting a large number of tasks and you would like your main process to fail as soon as one of your tasks fail, you can use the following snippet:

    from concurrent.futures import ProcessPoolExecutor, wait, FIRST_EXCEPTION, as_completed
    import time
    
    
    def go():
        print(1)
        time.sleep(0.3)
        raise Exception()
        print(2)
    
    
    futures = []
    with ProcessPoolExecutor(1) as p:
        for i in range(10):
            futures.append(p.submit(go))
    
        for f in as_completed(futures):
            if f.exception() is not None:
                for f in futures:
                    f.cancel()
                break
    
    [f.result() for f in futures]
    

    All of the other answers fail only once all tasks have been executed.

提交回复
热议问题