获取多处理终止 map_async 的结果

get results of terminated map_async of multiprocessing

我是 运行 具有多处理功能的脚本 map_async。我需要做的是在用 terminate() 终止 Pool 后得到 AsyncResult 对象的不完整结果(假设它已经完成了给定的一些情况的计算)。使用 get() 只会挂起脚本,我该怎么做?

我知道这可以通过 apply_sync 通过一些操作来完成,但是可以通过 map_async 以某种方式完成吗?

情况的工作示例:

import multiprocessing
import time
def example_run(i):
    time.sleep(0.7)
    return i

if __name__ == '__main__':
    terminate = False
    pool = multiprocessing.Pool(10)
    result_async = pool.map_async(example_run,range(100))
    i = 0
    while True:
        time.sleep(1.0)
        if i == 70:
            terminate = True
        print(result_async.ready(),terminate)
        if result_async.ready():
            break
        elif terminate:
            pool.terminate()
            break
        i += 10
    result = result_async.get() # The problem is here, it will just wait
    print(result)
    pool.close()
    pool.join()

我找到了解决问题的方法;通过一些挖掘,AsyncResult._value 似乎保留了执行的值,None 以防尚未评估

import multiprocessing
import time
def example_run(i):
    time.sleep(0.7)
    return i

if __name__ == '__main__':
    terminate = False
    pool = multiprocessing.Pool(10)
    result_async = pool.map_async(example_run,range(100))
    i = 0
    while True:
        time.sleep(1.0)
        if i == 70:
            terminate = True
        print(result_async.ready(),terminate)
        if result_async.ready():
            break
        elif terminate:
            pool.terminate()
            break
        i += 10
    result = []
    for value in result_async._value:
        if value is not None:
            result.append(value)
        else:
            result.append("failed")
    print(result)
    pool.close()
    pool.join()