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