複数のスレッドで同じ関数を開始する Python スクリプトを実行しています。この関数は、2 つのカウンター (c1 と c2) を作成して処理します。フォークされたプロセスからのすべての c1 カウンターの結果は、一緒にマージする必要があります。異なるフォークによって返されるすべての c2 カウンターの結果と同じです。
私の(疑似)コードは次のようになります。
def countIt(cfg)
c1 = Counter
c2 = Counter
#do some things and fill the counters by counting words in an text, like
#c1= Counter({'apple': 3, 'banana': 0})
#c2= Counter({'blue': 3, 'green': 0})
return c1, c2
if __name__ == '__main__':
cP1 = Counter()
cP2 = Counter()
cfg = "myConfig"
p = multiprocessing.Pool(4) #creating 4 forks
c1, c2 = p.map(countIt,cfg)[:2]
# 1.) This will only work with [:2] which seams to be no good idea
# 2.) at this point c1 and c2 are lists, not a counter anymore,
# so the following will not work:
cP1 + c1
cP2 + c2
上記の例に従って、次のような結果が必要です: cP1 = Counter({'apple': 25, 'banana': 247, 'orange': 24}) cP2 = Counter({'red': 11, 'blue': 56、「緑」: 3})
私の質問: 親プロセスの各カウンター (すべての c1 とすべての c2) を集計するために、フォークされたプロセスの洞察をどのようにカウントできますか?