###やりたいこと
multiprocessingで並列処理を行った計算結果をstackに追加したいです。
###ソースコード
from multiprocessing import Pool, cpu_count, current_process import time stack = [] cpu = cpu_count() * 2 def slowf(x): time.sleep(1) return x*x if __name__ == '__main__': st = time.time() p = Pool(cpu) stack.append(p.map(slowf, range(1,11))) print stack print('cpu : %d' % cpu_count()) print ('stack count : %d' % len(stack)) print('time : %.3f s' % (time.time()-st))
###発生している問題
http://matsulib.hatenablog.jp/entry/2014/06/19/001050を参考にプログラムを書いてみました。
並列で計算は行えているようですが、stackに計算結果を代入する所で1要素として全ての計算結果が代入されてしまいました。
・結果
[[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]] cpu : 4 stack count : 1 time : 2.328 s
・なって欲しい結果(以下は手書きです)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100] cpu : 4 stack count : 10 time : 2.328 s
どうしてもstack(もしくはqueue)に結果を入れたいのですが、何か良い案はございますでしょうか。。。。
###補足情報(
実行環境:windows 7
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/31 14:34
2016/10/31 16:03
2016/11/01 00:50