Hot reloading
pbrissaud opened this issue · 5 comments
Hi,
I was wondering if this cool library can perform hot reloading to update config in live if the file changed.
Eg:
main.py
import confuse
import sys
import time
if __name__ == "__main__":
config = confuse.Configuration('app', __name__)
print(config)
time.sleep(20)
print(config)
sys.exit(0)
~/.config/app/config.yaml
foo: bar
I launch the program, then I modify the config.yaml
file to
foo: bar
test: hello world
Program output:
$ python3 main.py
{'foo': 'bar'} # I change the config.yaml file after this output
{'foo': 'bar'} # Sould be {'foo': 'bar', 'test': 'hello world'}
# Relaunching program to be sure
% python3 main.py
{'foo': 'bar', 'test': 'hello world'}
{'foo': 'bar', 'test': 'hello world'}
Thanks
Yo! We don't have anything that automatically watches for on-disk file changes (that's a little complicated to do in a cross-platform way), but you can consider manually reloading sources when stuff might have changed. Something like this (not that I've tested it):
for source in config.sources:
if isinstance(source, confuse.sources.YamlSource):
source.load()
Thanks for your quick answer, I will look to your solution !
Your solution works like a charm, a big thanks ! Any plans to integrate this as a builtin method in the library (eg: config.reload()
) ?
I certainly have no objection to adding such a thing! Would you be interested in opening a pull request?
Yeah of course !