how can I share a dictionary across multiple proce

2019-08-11 01:08发布

问题:

I was wondering if it's possible to share the contents of a dictionary across multiple processes. I've been looking at http://docs.python.org/2/library/multiprocessing.html#shared-ctypes-objects but it only describes how to share variables but I haven't figured out how I can share a complete dictionary. I know, I could use pickle to share it via storing it to a file but that seems to not be very efficient esp. cause I'm running this on a system with flash memory... any tips?

Thanks, Ron

回答1:

Use manager from the multiprocessing library.

from multiprocessing import Process, Manager

def f(d):
    for i in range(10000):
        d['blah'] += 1

if __name__ == '__main__':
    manager = Manager()

    d = manager.dict()
    d['blah'] = 0
    procs = [ Process(target=f, args=(d,)) for _ in range(10) ]
    for p in procs:
        p.start()
    for p in procs:
        p.join()

    print d