ValueError: 'Pool not running' when trying to loop the multiprocessing pool

ValueError: 'Pool not running' when trying to loop the multiprocessing pool

我正在尝试 运行 同一池三次,以便将值添加到同一列表中:

import multiprocessing

def foo(name,archive):
    print('hello ', name)
    archive.append(f"hello {name}")

def main():
    max_process = multiprocessing.cpu_count()-1 or 1
    pool = multiprocessing.Pool(max_process)
    manager = multiprocessing.Manager()

    archive = manager.list()

    arguments = ['home','away','draw']
    for _ in range(3):
        [pool.apply_async(foo, args=[name,archive]) for name in arguments]
        pool.close()
        pool.join()
    print(archive)

if __name__ == '__main__':
    main()

第一批运行完美,但是第二批走的时候出现这个错误:

ValueError: Pool not running

我应该如何继续生成这个循环?

正如 Nullman 在评论中指出的那样,错误在于将 pool.close()pool.join() 保留在循环中,将它们带出,效果很好:

import multiprocessing

def foo(name,archive):
    print('hello ', name)
    archive.append(f"hello {name}")

def main():
    max_process = multiprocessing.cpu_count()-1 or 1
    pool = multiprocessing.Pool(max_process)
    manager = multiprocessing.Manager()

    archive = manager.list()

    arguments = ['home','away','draw']
    for _ in range(3):
        [pool.apply_async(foo, args=[name,archive]) for name in arguments]
    pool.close()
    pool.join()
    
    print(archive)

if __name__ == '__main__':
    main()