python - return counter object in multiprocessing / map function -


i have python script running, starts same function in multiple threads. functions creates , process 2 counters (c1 , c2). result of c1 counters forked processes should merged together. same results of c2 counters, returned different forks.

my (pseudo)-code looks that:

def countit(cfg)    c1 = counter    c2 = counter    #do things , fill counters counting words in text,    #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.) work [:2] seams no idea         # 2.) @ point c1 , c2 lists, not counter anymore,         # following not work:         cp1 + c1         cp2 + c2 

following example above, need result like: cp1 = counter({'apple': 25, 'banana': 247, 'orange': 24}) cp2 = counter({'red': 11, 'blue': 56, 'green': 3})

so question: how can count things insight forked process in order aggregate each counter (all c1 , c2) in parent process?

you need "unzip" result using example for-each loop. receive list of tuples each tuple pair of counters: (c1, c2).
current solution mix them up. assigned [(c1a, c2a), (c1b, c2b)] c1, c2 meaning c1 contains (c1a, c2a) , c2 contains (c1b, c2b).

try this:

if __name__ == '__main__':         contextlib import closing          cp1 = counter()         cp2 = counter()          # hope have actual list of configs here, otherwise map         # call `countit` single characters of string 'myconfig'         cfg = "myconfig"          # `contextlib.closing` makes sure pool closed after we're done.         # in python3, pool contextmanager , don't need         # surround `closing` in order able use in `with`         # construct.         # approach, however, compatible both python2 , python3.         closing(multiprocessing.pool(4)) p:             # counting, no need order results.             # might bit faster.             c1, c2 in p.imap_unordered(countit, cfg):                 cp1 += c1                 cp2 += c2 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -