所有示例 concurrent.futures 代码都因“BrokenProcessPool"而失败 [英] All example concurrent.futures code is failing with "BrokenProcessPool"

查看:454
本文介绍了所有示例 concurrent.futures 代码都因“BrokenProcessPool"而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建我需要的实际应用程序之前,我试图对此有一个基本的了解.我最近从 2.7 转到了 3.3.

I am trying to get a basic understanding of this before I create the actual application I need. I recently moved over from 2.7 to 3.3.

直接复制粘贴 python 文档中的此代码 失败了,这里中的一个稍微简单的例子也是如此.

A direct copy-paste of this code from the python docs fails, as does a slightly simpler example from here.

这是我的代码,来自第二个例子:

This is my code, derived from the second example:

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.

我怎样才能让这段代码按预期工作?我希望这些示例能够开箱即用.

How can I get this code to work as expected? I was hoping the examples would just work out of the box.

推荐答案

这是我的错,原因有二:

This was my fault, for two reasons:

  1. 代码未受保护,即没有 if __name__
  2. 看起来很奇怪的 Traceback 是因为文件没有保存.以前从未给我带来过问题,但在本例中却给我造成了问题.

更正两者修复了错误.

最终测试代码:

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]

这篇关于所有示例 concurrent.futures 代码都因“BrokenProcessPool"而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆