所有示例concurrent.futures代码都失败了“BrokenProcessPool”

时间:2013-04-09 11:14:29

标签: python python-3.x

在创建我需要的实际应用程序之前,我试图对此有一个基本的了解。我最近从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.

如何让此代码按预期工作?我希望这些例子能够开箱即用。

3 个答案:

答案 0 :(得分:17)

这是我的错,原因有两个:

  1. 代码没有防范,即没有if __name__
  2. 看起来奇怪的Traceback是因为文件没有保存。从来没有给我造成任何问题,但在这种情况下也是如此。
  3. 更正修复错误的

    最终测试代码:

    import concurrent.futures
    
    nums = [1,2,3,4,5,6,7,8,9,10]
    
    def f(x):
        return x * x
    def main():
        # 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:
            print([val for val in executor.map(f, nums)])
    
    if __name__ == '__main__':
        main()
    

    按预期输出:

    [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
    [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
    

答案 1 :(得分:6)

在Windows下,保护代码的主循环非常重要,以避免在使用processpoolexecutor或产生新进程的任何其他并行代码时递归生成子进程。

基本上,创建新进程的所有代码都必须在{product_name}之下,原因与您无法在解释器中执行它相同。

答案 2 :(得分:0)

这实际上在Windows上仍在发生。 我有Python 3.7.2,并尝试了以下文档中给出的示例 https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ProcessPoolExecutor

即使示例已经从__name__ == '__main__'下调用了新进程,该错误仍然会发生。

我在Windows 10和Windows 7上都尝试过。 它确实可以在MacOS Sierra的Mac上宣传。相同的python版本。