Python: 将 Pool 对象传递给不同进程中的函数

Python: passing Pool objects to a function in different process

我在以下代码中做错了什么?

import multiprocessing as mp
from multiprocessing import Process
import numpy as np

def fun(X):
    return X;

def funct(p,i):
    print 'Hey'
    res = [p.map(fun,range(2))];
    return res;


if __name__ == '__main__':
    pool = mp.Pool(2);
    output = mp.Queue();
    proc = [mp.Process(target = funct,args=(pool,i)) for i in range(2)]
    for p in proc:
        p.start()
    for p in proc:
        p.join()
    results = [output.get() for p in proc]
    print results

启动进程后程序没有运行。

multiprocessing.Pool 不支持传递给子进程,如 multiprocessing documentation:

中所述

Note that the methods of a pool should only ever be used by the process which created it.

所以实际上没有办法做你正在尝试的事情。我假设你上面的代码只是一个演示问题的例子,所以我不会直接对此发表评论,但你需要重构你的实际代码以仅从父进程调用池方法(或者可能创建池本身,但这可能不是一个好的解决方案)。