我有一个 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)?