jupyter笔记本中的多处理.Pool适用于Linux而不适用于windows

时间:2016-05-08 18:17:32

标签: python python-2.7 python-multiprocessing jupyter

我正在尝试运行一些独立的计算(尽管从相同的数据中读取)。我的代码在Ubuntu上运行时有效,但在Windows(Windows Server 2012 R2)上没有,我得到错误:

'module' object has no attribute ...

当我尝试使用multiprocessing.Pool时(它出现在内核控制台中,而不是笔记本本身的输出)

(我已经犯了在创建池后定义函数的错误,我也纠正了它,这不是问题所在。)

即使是最简单的例子,也会发生这种情况:

from multiprocessing import Pool
def f(x):
    return x**2
pool = Pool(4)
for res in pool.map(f,range(20)):
    print res

我知道它需要能够导入模块(我不知道在笔记本中工作时它是如何工作的),我听说过IPython.Parallel,但我一直无法找到任何文件或例子。

任何解决方案/替代品都是最受欢迎的。

1 个答案:

答案 0 :(得分:2)

我会将此作为评论发布,因为我没有完整的答案,但在我弄清楚发生了什么时我会修改。

from multiprocessing import Pool

def f(x):
    return x**2

if __name__ == '__main__':
    pool = Pool(4)
    for res in pool.map(f,range(20)):
        print(res)

这很有效。我相信这个问题的答案是here。简而言之,子进程不知道它们是子进程并且正在尝试递归地运行主脚本。

这是我给出的错误,它给了我们相同的解决方案:

RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.