Search code examples
pythonpython-3.xmultiprocessingpython-multiprocessingmultiprocessing-manager

Python sharing multiprocessing.Manager.list with child processes


My question is so simple. I want to share a multiprocessing.Manager().dict() with child processes. But shared dict will be initialized AFTER starting childs. Here is the example code:

import multiprocessing
import time

class Singleton:
    _instance = None
    _lock = multiprocessing.Lock()
    dns_list = multiprocessing.Manager().dict()

    def __new__(cls):
        with cls._lock:
            if cls._instance is None:
                cls._instance = super().__new__(cls)
        return cls._instance

def worker1(singleton_object):
    i = 0
    while i in range(25):
        i += 1
        print(singleton_object.dns_list)
        time.sleep(1)


singleton = Singleton()
p1 = multiprocessing.Process(target=worker1, args=(singleton,))
p1.start()

singleton.dns_list = {'1.1.1.1': {'status': True}, '2.2.2.2': {'status': True}}

In my real code, I have multiple processes running. One of them changes the dns_list but the other ones don't recieve the updated list. I've tried to use event but didn't worked as well. I need to see that printing the variable should change to {'1.1.1.1:....} from {}. If I can run this simple code, I can manupulate it into my code :D

Thanks for every comment.


Solution

  • Thanks to Spencer Pollock for showing the solution. In the code I updated the dict in the wrong way.

    dns_list = multiprocessing.Manager().dict()
    
    # This is wrong because the the of the dns_list will be dict()
    dns_list = ({'1.1.1.1': {'status': True}, '2.2.2.2': {'status': True}})
    
    # This is correct way to change multiprocessing.Manager().dict() object
    dns_list.update({'1.1.1.1': {'status': True}, '2.2.2.2': {'status': True}})
    
    # To clear the list:
    dns_list.clear()