在创建我需要的实际应用程序之前,我试图对此有一个基本的了解。我最近从2.7改到3.3。
直接复制粘贴this code from the python docs失败,就像here中的一个稍微简单的示例一样。
这是我的代码,来自第二个示例:
import concurrent.futures
nums = [1,2,3,4,5,6,7,8,9,10]
def f(x):
return x * x
# Make sure the map and function are working
print([val for val in map(f, nums)])
# Test to make sure concurrent map is working
with concurrent.futures.ProcessPoolExecutor() as executor:
for item in executor.map(f, nums):
print(item)
这是输出:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Traceback (most recent call last):
File "<string>", line 420, in run_nodebug
File "<module1>", line 13, in <module>
File "C:\Python33\lib\concurrent\futures\_base.py", line 546, in result_iterator
yield future.result()
File "C:\Python33\lib\concurrent\futures\_base.py", line 399, in result
return self.__get_result()
File "C:\Python33\lib\concurrent\futures\_base.py", line 351, in __get_result
raise self._exception
concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.
如何使此代码按预期工作?我希望这些例子能开箱即用。
这是我的错,原因有二:
if __name__
修正了这两个错误。
最终测试代码:
产出,如预期:
在Windows下,当使用processpoolexecutor或任何其他生成新进程的并行代码时,保护代码的主循环以避免子进程的递归生成非常重要。
基本上,所有创建新进程的代码都必须在
if __name__ == '__main__':
下,因为同样的原因,您不能在解释器中执行它。相关问题 更多 >
编程相关推荐