我有一個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 =計數器({ '蘋果':25, '香蕉':247, 'orange':24}) cP2 = Counter({'red':11,'blue':56,'green':3})
所以我的問題:我該如何計算事物洞察分叉過程爲了彙總父進程中的每個計數器(全部是c1和全部c2)?
@mattm這是行不通的,因爲'總和()'不會返回櫃檯?以下錯誤發生:'TypeError:不支持的操作數類型爲+:'int'和'Counter'' –
至少這行肯定是一個錯誤:'c1,c2 = p.map(countIt,cfg)[ :2]'。你可以看到如何處理swenzel的答案的結果。 – KobeJohn