首页 > 解决方案 > 如何停止 multiprocessing.Pool.map 异常

问题描述

当我在 my 中引发异常时thread_function,它不会停止其余的map处理。我想阻止它。

def thread_function(n):
    if n == 10:
        raise Exception('Stop everything!')

pool = Pool(processes = 4)
pool.map(thread_function, range(1, 1000), chunksize = 1)

我希望在一个线程到达后不再进行处理n == 10

标签: python

解决方案


我不知道直接使用的方法,map但是您可以async_map像这样监视...

from multiprocessing import Pool
import time

def thread_function(n):
    if n == 10:
        print('Raising Exception')
        raise Exception('Stop everything!')
    print(n)
    time.sleep(0.1)

pool = Pool(processes = 4)
result = pool.map_async(thread_function, range(1, 1000), chunksize = 1)
while not result.ready():
    if not result._success:
        print('Exiting for failure')
        pool.terminate()
        pool.join()
        break

推荐阅读