我正在尝试遵循一个非常简单的多处理示例:
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.
这篇关于Python 多处理 apply_async 永远不会在 Windows 7 上返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
Python 多处理模块的 .join() 方法到底在做什么?What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多处理模块的 .join() 方法到底在做什么?)
在 Python 中将多个参数传递给 pool.map() 函数Passing multiple parameters to pool.map() function in Python(在 Python 中将多个参数传递给 pool.map() 函数)
multiprocessing.pool.MaybeEncodingError: 'TypeError("multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEnc
Python 多进程池.当其中一个工作进程确定不再需要Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多进程池.当其中一
如何将队列引用传递给 pool.map_async() 管理的函数How do you pass a Queue reference to a function managed by pool.map_async()?(如何将队列引用传递给 pool.map_async() 管理的函数?)
与多处理错误的另一个混淆,“模块"对象没yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(与多处理错误的另一个混淆,“模块对象