Python-dynamically modify the configuration file (importlib.reload)

Author : xuzhiping   2022-11-14 14:18:36 Browse: 1301
Category : Python

Abstract: Project Scenarios After the project development is completed, there is a need to dynamically modify the project configuration fi...

Project Scenarios

Python

After the project development is completed, there is a need to dynamically modify the project configuration file. What does dynamic mean? You can think that our project is always running, and the project operation depends on a certain configuration file. Our goal is to modify the configuration file without affecting the normal operation of the project, and the modified project uses a new configuration file.

Problem analysis

Keep the project running and use while true to simulate. The problems we want to solve can be divided into two parts:

  • Modify the configuration file without affecting the project operation.
  • Enable projects to be loaded into new configuration files.

Solution

The format of the configuration file is different and the solution is different. The recommended configuration file format in Python is: YAML And PY .

YAML

config.yaml

params:
A: 1
B: 2

The YAML configuration file needs to solve the problem of file occupation. Because the project needs to keep running, when the YAML configuration file is modified, the project cannot load the configuration file, and an exception is thrown. The following code is used to load the YAML configuration file:

import yaml # pip intall yaml
params = yaml.load (open ('config.yaml'), Loader=yaml.FullLoader)

Delve into yaml.load() The source code of the function shows that when the configuration file is in an occupied state, no parameters are loaded, and it returns None . In yaml.load() A coat. while Cycle, when params Not for None The problem of file occupation can be solved by ending the loop when the configuration file has been modified.

We can learn from the source code of yaml. load() function, when the configuration file is in the occupied state, if no parameters can be loaded, None will be returned.

In yaml Load () is wrapped in a while loop. When params is not None (that is, the configuration file is modified), the loop is ended to solve the file occupation problem.

app.py

import yaml
while True:
params = None
while params = = None:
params = yaml.load (open ('config.yaml'), Loader=yaml.FullLoader)
print (params)

PY

config.py

params = {
'params': {
'asides: 1
'baked: 2
}
}

PY configuration file is to solve the overload problem. 'from config import params' is imported only once by default, and modify the params variable in config.py has no effect on the params variable in the running app.py. We need to reload the config.py file each time to monitor the update of the configuration parameters, which is implemented using importlib.reload.

app.py

from importlib import reload
import config
while True:
reload (config)
from config import params
print (params)
Label :
    Sign in for comments!
Comment list (0)

Powered by TorCMS (https://github.com/bukun/TorCMS).