ランダムな方法でnumpy配列と画像を同時に処理するpythonスクリプトがあります。生成されたプロセス内で適切なランダム性を持たせるために、メイン プロセスからワーカーにランダム シードを渡して、シードを与えます。
を使用するmaxtasksperchildと、何度もPool実行した後にスクリプトがハングしますPool.map。
以下は、問題を再現する最小限のスニペットです。
# This code stops after multiprocessing.Pool workers are replaced one single time.
# They are replaced due to maxtasksperchild parameter to Pool
from multiprocessing import Pool
import numpy as np
def worker(n):
# Removing np.random.seed solves the issue
np.random.seed(1) #any seed value
return 1234 # trivial return value
# Removing maxtasksperchild solves the issue
ppool = Pool(20 , maxtasksperchild=5)
i=0
while True:
i += 1
# Removing np.random.randint(10) or taking it out of the loop solves the issue
rand = np.random.randint(10)
l = [3] # trivial input to ppool.map
result = ppool.map(worker, l)
print i,result[0]
これが出力です
1 1234 2 1234 3 1234 . . . 99 1234 100 1234 # この時点でワーカーは maxtasksperchild タスクに達しているはずです 101 1234 102 1234 103 1234 104 1234 105 1234 106 1234 107 1234 108 1234 109 1234 110 1234
その後、無期限にハングします。
私は潜在的にnumpy.randompythonのものに置き換えrandomて、問題を解決することができました。しかし、私の実際のアプリケーションでは、ワーカーは (ワーカーへの引数として与えられた) ユーザー コードを実行しますが、これは制御できずnumpy.random、そのユーザー コードで関数を使用できるようにしたいと考えています。そのため、意図的にグローバルランダムジェネレーターをシードしたいと考えています(各プロセスに対して個別に)。
これは、Python 2.7.10、numpy 1.11.0、1.12.0 & 1.13.0、Ubuntu、OSX でテスト済みです。