简体   繁体   English

python / multiprocessing.pool中的错误?

[英]Bug in python/multiprocessing.pool?

(this is using Python 2.7) (这是使用Python 2.7)

I have found similar links but not about the exact same issue than what I am having. 我找到了类似的链接,但没有找到与我所拥有的完全相同的问题。 This program hangs on the map_async, and never finishes, I can see the Python process getting created but it never completes: 这个程序挂在map_async上,永远不会完成,我可以看到Python进程被创建但它永远不会完成:

import multiprocessing


def main():
    PROCESSES = 4
    print 'Creating pool with %d processes\n' % PROCESSES
    pool = multiprocessing.Pool(PROCESSES)
    r = pool.map_async(pow3, range(10))
    r.wait()


def pow3(x):
    try:
        return x**3
    except:
        print('%s: %s' % (x, traceback.format_exc()))

if __name__ == '__main__':
    main()

It works fine. 它工作正常。
Are you running this in an interactive interpreter? 你是在交互式翻译中运行吗?
See this note from the docs : 请参阅文档中的此说明:

Note 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. 这意味着某些示例(例如multiprocessing.Pool示例)在交互式解释器中不起作用。 For example: 例如:

>>> from multiprocessing import Pool   
>>> p = Pool(5)   

>>> def f(x):   
...     return x*x  

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

Process PoolWorker-1: 进程PoolWorker-1:
Process PoolWorker-2: 进程PoolWorker-2:
Process PoolWorker-3: 进程PoolWorker-3:

Traceback (most recent call last): Traceback(最近一次调用最后一次):

AttributeError: 'module' object has no attribute 'f' AttributeError:'module'对象没有属性'f'
AttributeError: 'module' object has no attribute 'f' AttributeError:'module'对象没有属性'f'
AttributeError: 'module' object has no attribute 'f' AttributeError:'module'对象没有属性'f'

(If you try this it will actually output three full tracebacks interleaved in a semi-random fashion, and then you may have to stop the master process somehow.) (如果你尝试这个,它实际上会以半随机的方式输出三个完整的回溯,然后你可能不得不以某种方式停止主进程。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM