0

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.

Syrenthia
  • 145
  • 2
  • 11
  • 1
    Is there a hard requirement to create the list after starting the child processes? I think this might be best to create the list before, to know that the list is available to the child processes before the children start on the CPU. You're also overwriting the singleton.dns_list in the last line - instead, you should update the dictionary `singleton.dns_list.update({'key': 'value})` – Spencer Pollock Feb 17 '23 at 08:56
  • 1
    Hi there. Yes, because in my design one of the child process retrieves the dns list from server and shares with others. I don't know the list in my main process. Your last advice solved my problem actually. I checked the `dns_list` type twice and saw `dict()` which should be something like "proxy". I must "update" the dict as you said. Thank your for comment! – Syrenthia Feb 17 '23 at 10:37

1 Answers1

1

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()
Syrenthia
  • 145
  • 2
  • 11