Exception thrown in multiprocessing Pool not detected

后端 未结 9 828
灰色年华
灰色年华 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 you have used apply_sync, I guess the use case is want to do some synchronize tasks. Use callback for handling is another option. Please note this option is available only for python3.2 and above and not available on python2.7.

    from multiprocessing import Pool
    
    def callback(result):
        print('success', result)
    
    def callback_error(result):
        print('error', result)
    
    def go():
        print(1)
        raise Exception()
        print(2)
    
    p = Pool()
    p.apply_async(go, callback=callback, error_callback=callback_error)
    
    # You can do another things
    
    p.close()
    p.join()
    

提交回复
热议问题