Multiprocessing Pool terminate give me a AssertionError

When I used multiprocessing Pool, when I used terminate to close the process, the pool raised an AssertionError: Cannot have cache with result_hander not alive, but I didn’t know what happened.

key code


error_exit: bool = False

def error_callback(e):
    global error_exit
    error_exit = True
    raise e

def main():
    arg = get_arg()
    logging.warning('Processing start!')
    p = psutil.Process()
    cpu_num = len(p.cpu_affinity())
    proc_manager: Manager = Manager()
    proc_list = proc_manager.list([0 for _ in range(cpu_num * 2)])
    msg_queue: Queue = proc_manager.Queue()
    start = time.time()
    with Pool(cpu_num) as pool:
        for local_rank in range(cpu_num):
            pool.apply_async(process_call,
                             (local_rank, cpu_num, arg, proc_list, msg_queue),
                             error_callback=error_callback)

        while True:
            if error_exit:
                break
            time.sleep(1)
            total_finish = sum(proc_list[0:cpu_num])
            total_time = time.time() - start
            print(f"\r{total_finish:>08}, "
                  f"total time is {total_time:08.2f}, "
                  f"iters/s sample is {total_finish / total_time :05.2f}")
            finish_flag = sum(proc_list[cpu_num:])

            while not msg_queue.empty():
                msg = msg_queue.get()
                logging.warning(msg)

            if finish_flag == cpu_num:
                break
        if error_exit:
            pool.terminate()
        else:
            pool.close()
        pool.join()
    logging.warning('Processing finish!')