5
我想写测试nose,得到的东西 设置使用多处理计算。测试蟒蛇多处理池鼻子的代码
我有这样的目录结构:
code/
tests/
tests.py
tests.py看起来是这样的:
import multiprocessing as mp
def f(i):
return i ** 2
pool = mp.Pool()
out = pool.map(f, range(10))
def test_pool():
"""Really simple test that relies on the output of pool.map.
The actual tests are much more complicated, but this is all
that is needed to produce the problem."""
ref_out = map(f, range(10))
assert out == ref_out
if __name__ == '__main__':
test_pool()
从code
目录中运行,python tests/tests.py
通过。
nosetests tests/tests.py
失败来完成。它启动,但从来没有通过呼叫pool.map
,只是挂起。
为什么这是最简单的解决方案?
这可能是'nose'使用一些线程和/或运行时,记录试验。当*在UNIX系统上与多重处理混合时,*会导致死锁。这不是python实现的问题,而是使用'fork()'函数本身,它只分析当前线程,请参阅[this](http://stackoverflow.com/questions/6078712/is-it-safe-to -for-within-a-thread/6079669#6079669)回答更详细的解释。 – Bakuriu
我相信唯一的(?)解决方案就是模拟'multiprocessing'模块。事实上,我没有看到你的例子正在测试什么。它实际上是'multiprocessing.Pool.map'方法的单元测试,而不是'f'函数! – Bakuriu
这是最小的例子,再现我的错误。我正在测试一些使用'pool.map'的结果作为输入的其他东西。 – aaren