多处理在Python中工作但在iPython中不工作 [英] Multiprocessing working in Python but not in iPython
问题描述
我在iPython中运行以下代码:
I am running the following code in iPython:
import multiprocessing
def my_function(x):
"""The function you want to compute in parallel."""
x += 1
return x
if __name__ == '__main__':
pool = multiprocessing.Pool()
results = pool.map(my_function, [1,2,3,4,5,6])
print(results)
。但是,代码不起作用 - QT控制台只是冻结了。这个问题是iPython特有的(上面的代码应该适用于常规的Python 2.7)。
in ipython QT console on Windows. However, the code does not work -- the QT console just freezes up. The issue is specific to iPython (the code above should work for the regular Python 2.7).
任何解决方案?
推荐答案
来自文档:
注意
Note
此软件包中的功能要求 __ main __
模块为
可由儿童。这在编程指南
中有所涉及,但值得指出。 这意味着一些示例,
,例如multiprocessing.Pool示例将无法在
交互式解释器中使用
Functionality within this package requires that the __main__
module be
importable by the children. This is covered in Programming guidelines
however it is worth pointing out here. This means that some examples,
such as the multiprocessing.Pool examples will not work in the
interactive interpreter
这篇关于多处理在Python中工作但在iPython中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!