2016-04-25 13 views
0

私はマルチプロセッシングモジュールに問題があります。私はforループを使ってdictを繰り返したいので、プロセスはすべてのdictitemの仕事をするべきです。マルチプロセッシングでこれを行う最良の方法は何ですか?Python3 for looping for multiprocessing

答えて

1

the documentation of the multiprocessing moduleにはかなりわかりやすい例があります。次のコードは、最初の1に基づいており、f()はあなたがすべての辞書項目に対して実行機能である:

from multiprocessing import Pool 

def f(x): 
    return (x[0], x[1]*x[1]) 

if __name__ == '__main__': 
    p = Pool(5) 
    d = {'a':1, 'b':2, 'c':3} 
    print list(d.iteritems()) 
    print(p.map(f, d.iteritems())) 

リターン:あなたの助けを

[('a', 1), ('c', 3), ('b', 2)] 
[('a', 1), ('c', 9), ('b', 4)] 
+0

ありがとう!それはまさに私が探しているものです! – Melody