Python 多处理 apply_async 永远不会在 Windows 7 上返回结果

2022-01-12 00:00:00 python windows multiprocessing

问题描述

我正在尝试遵循一个非常简单的多处理示例:

I am trying to follow a very simple multiprocessing example:

import multiprocessing as mp

def cube(x):
    return x**3

pool    = mp.Pool(processes=2)
results = [pool.apply_async(cube, args=x) for x in range(1,7)]

但是,在我的 windows 机器上,我无法获得结果(在 ubuntu 12.04LTS 上它运行完美).

However, on my windows machine, I am not able to get the result (on ubuntu 12.04LTS it runs perfectly).

如果我检查 results,我会看到以下内容:

If I inspect results, I see the following:

[<multiprocessing.pool.ApplyResult object at 0x01FF0910>,
 <multiprocessing.pool.ApplyResult object at 0x01FF0950>,
 <multiprocessing.pool.ApplyResult object at 0x01FF0990>,
 <multiprocessing.pool.ApplyResult object at 0x01FF09D0>,
 <multiprocessing.pool.ApplyResult object at 0x01FF0A10>,
 <multiprocessing.pool.ApplyResult object at 0x01FF0A50>]

如果我运行 results[0].ready() 我总是得到 False.

If I run results[0].ready() I always get False.

如果我运行 results[0].get(),python 解释器会冻结,等待得到永远不会出现的结果.

If I run results[0].get() the python interpreter freezes, waiting to get the result that never comes.

这个例子很简单,所以我认为这是一个与操作系统相关的低级错误(我在 Windows 7 上).但也许其他人有更好的主意?

The example is as simple as it gets, so I am thinking this is a low level bug relating to the OS (I am on Windows 7). But perhaps someone else has a better idea?


解决方案

这里有几个错误.首先,您必须在 if __name__ == "__main__": 中声明 Pool 保护 在 Windows 上运行时.其次,您必须将 args 关键字参数传递一个序列,即使您只传递一个参数.所以把它们放在一起:

There are a couple of mistakes here. First, you must declare the Pool inside an if __name__ == "__main__": guard when running on Windows. Second, you have to pass the args keyword argument a sequence, even if you're only passing one argument. So putting that together:

import multiprocessing as mp

def cube(x):
    return x**3

if __name__ == "__main__":
    pool    = mp.Pool(processes=2)
    results = [pool.apply_async(cube, args=(x,)) for x in range(1,7)]
    print([result.get() for result in results])

输出:

[1, 8, 27, 64, 125, 216]

哦,正如 moarningsun 所提到的,multiprocessing 效果不佳 在交互式解释器中:

Oh, as moarningsun mentions, multiprocessing does not work well in the interactive interpreter:

注意

此包中的功能要求 __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.

因此,您需要将代码作为脚本实际执行才能正确测试.

So you'll need to actually execute the code as a script to test it properly.

相关文章