Python: multiprocessing.map: If one process raises an exception, why aren't other processes' finally blocks called?

后端 未结 3 1459
灰色年华
灰色年华 2021-02-01 04:12

My understanding is that finally clauses must *always* be executed if the try has been entered.

import random         


        
3条回答
  •  遇见更好的自我
    2021-02-01 04:52

    finally re-raises the original exception unless you return from it. The exception is then raised by Pool.map and kills your entire application. The subprocesses are terminated and you see no other exceptions.

    You can add a return to swallow the exception:

    def Process(x):
      try:
        print x
        sleep(random.random())
        raise Exception('Exception: ' + x)
      finally:
        print 'Finally: ' + x
        return
    

    Then you should have None in your map result when an exception occurred.

提交回复
热议问题