use*_*728 16 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
Run Code Online (Sandbox Code Playgroud)
我知道它需要能够导入模块(我不知道在笔记本中工作时它是如何工作的),我听说过IPython.Parallel,但我一直无法找到任何文档或示例.
任何解决方案/替代品都是最受欢迎的.
我会将其作为评论发布,因为我没有完整的答案,但当我弄清楚发生了什么时,我会进行修改。
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)
Run Code Online (Sandbox Code Playgroud)
这有效。我相信这个问题的答案就在这里。简而言之,子进程不知道它们是子进程,并尝试递归运行主脚本。
这是我遇到的错误,它为我们提供了相同的解决方案:
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.
Run Code Online (Sandbox Code Playgroud)