2

I wrote some code like this:

from multiprocessing import Process, Manager

def f(m, d, i):
    d['d'] = m.dict()
    d['d'][i] = i

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

    d = manager.dict()

    for i in range(10):
        p = Process(target=f, args=(manager, d, i))
        p.start()
        p.join()

    print d

And I pretended to get the following result:

{'d': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}}

but it didn't work and I got:

{'d': {}}

I am very confused about this and searched via google and find this website -- http://bugs.python.org/issue6766

Is it an existed issue or fixed? And what can I do to realize this function?

4
  • The issue has "open" status in the bug tracker. Nov 4, 2013 at 8:16
  • 1
    Take a look at the Manager documentation. Scroll a little down, and you'll see a note: "Modifications to mutable values or items in dict and list proxies will not be propagated through the manager, because the proxy has no way of knowing when its values or items are modified. To modify such an item, you can re-assign the modified object to the container proxy." Perhaps you'll have to re-assign the sub-dictionary within d
    – richizy
    Feb 7, 2014 at 23:03
  • E.g: subdict = m.dict(), subdict[i] = i, and finally, d['d'] = subdict
    – richizy
    Feb 7, 2014 at 23:05
  • Take a look at Pipes and Queues for inter-process communication.
    – clemens
    Dec 20, 2017 at 6:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.