問題描述
我正在 Ubuntu 14.04 上使用 Python 3.4 進行開發.我試圖做遞歸 Pool.map()
.在我調用 g()
之后,它掛在那里并且永遠不會返回.
I'm developing with Python 3.4 on Ubuntu 14.04. I was trying to do recursive Pool.map()
. After I invoke g()
, it hangs there and never returns.
import multiprocessing as mp
pool = mp.Pool()
def d(x):
return x / 2.0
def f(x):
w = pool.map(d, x)
return w
def g():
v = pool.map(f, [[1, 2], [3, 4]])
print(v)
推薦答案
這是不可能的.Pool
對象本身不能安全地在進程之間共享,因此不能在 f
和 g
中使用同一個池.即使您可以這樣做,也會很快導致掛起,因為您的池僅限于 cpu_count()
個并發工作人員.一旦你開始遞歸地創建更多的工人,你最終會得到超過 cpu_count()
個工人,這將永遠無法完成;正在運行的工作人員將等待池中排隊的任務,但排隊的任務將永遠無法啟動,因為正在運行的任務正在等待.所以你最終陷入僵局.簡而言之:不要嘗試這樣做.
This isn't possible. The Pool
object itself can't safely be shared between processes, so the same pool can't be used in both f
and g
. Even if you could do this, you'd quickly cause a hang, because your pool is limited to cpu_count()
concurrent workers. Once you start creating more workers recursively, you'll end up with more than cpu_count()
workers, which will never be able to finish; the running workers would be waiting on tasks that are queued up in the pool, but the queued up tasks won't ever be able to start because the running tasks are waiting. So you end up deadlocked. In short: don't try to do this.
這篇關于Python 3.4 多處理遞歸 Pool.map()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!