I've a project in gRPC where the main.py
spawns grpc servers as subprocesses.
Also in the project I've settings.py
that contains some configurations, like:
some_config = {"foo": "bar"}
In some files (used by different processes) I have:
import settings
...
the value of settings.some_config is read
In the main process I've a listener that updates some_config
on demand, for example:
settings.some_config = new_value
I noticed that while changing settings.some_config
value in the main process, it was not changed in a subprocess that I checked, and remained the old value.
I want that all subprocess would always have the most up-to-date value of settings.some_config
.
A solution I thought about - passing a queue or a pipe to each sub process, and when some_config
changes in the main process, I can send the new data through the queue/pipe to each subprocess.
But how can I alert it to assign new value to settings.some_config
in the subprocess? Should I use a listener in each subprocesses so that when a notification arrives it will do:
settings.some_config = new_value
Would this work? The end goal is to have settings.some_config
value the most up to date across all modules/process without restarting the server. I'm also not sure if it would work since it could be that each module keeps the value of settings.some_config
which was previously imported in its cached memory.
UPDATE
I took on Charchit's solution and adjusted it to my requirements, so we have:
from multiprocessing.managers import BaseManager, NamespaceProxy
from multiprocessing import Process
import settings
import time
def get_settings():
return settings
def run(proxy_settings):
settings = proxy_settings # So the module settings becomes the proxy object
if __name__ == '__main__':
BaseManager.register('get_settings', get_settings, proxytype=NamespaceProxy)
manager = BaseManager()
manager.start()
settings = manager.get_settings()
p = Process(target=run, args=(settings, ))
p.start()
Few questions:
Should an entire module (settings
) be the target of a proxy object? Is it standard to do so?
There is a lot of magic here, for instance, Is the simple answer, to how it works is that now the module settings is a shared proxy object? So when a sub process reads settings.some_config
, it would actually read the value from manager?
Are there any side effects I should be aware of?
Should I be using locks when I change any value in settings in the main process?