2010-01-20 80 views
6

可能重复:
Multiprocessing launching too many instances of Python VM为什么python多处理模块导致CPU完全耗尽?

我试图用这个简单的代码片段的Python 2.6多处理模块。

from multiprocessing import Pool 
p = Pool(5) 
def f(x): 
    return x*x 

print p.map(f, [1,2,3]) 

但是,这段代码导致我的操作系统停止响应。看起来CPU太忙了。 我的代码有什么问题?

顺便说一句:看来多处理模块有点危险。我不得不重新启动我的电脑。

回答

7

根本没有保护入口点,所以每个子进程都试图启动相同的map调用等等(无穷大!)。请尝试以下操作:

if __name__ == "__main__": 
    print p.map(f, [1,2,3]) 

请查看模块的文档的this section

+0

@Trent:那不好:) – jkp 2010-01-20 15:20:41

+0

看到这个:http://stackoverflow.com/questions/1923706/multiprocessing-launching-too-many-instances-of-python-vm – 2010-01-20 16:48:24

相关问题