Ad

How To Update Config File On While Loop And Get Value From Different Script At Same Time?

Here is the scenario, I have two scripts lets say abc.py and xyz.py

using abc.py I want to update config file every other second. Here is the sample code.

ABC.PY

while True:
    cfgfile=config.read("config.ini")
    config.set('section','option',Value)
    with open('config.ini', 'w') as configfile:    
        config.write(configfile)
    time.sleep(1)

On Xyz.py I want to fetch the values from config.ini. my code on XYZ.PY

import configparser

file = input("Enter the file name: ")
config = configparser.ConfigParser()
cfgfile = config.read("config.ini")
values = config.get(file, 'option')
print(values)

But the problem is, the ABC.py is only updating the config file once! That means it is updating the file only on First While loop. It isn't updating the config file every second, as I thought it would.

Ad

Answer

In the ABC.py script, Value is not generated in the loop thus, every second you will write the same value in your config file. So it is normal that your second script read the same value.

Ad
source: stackoverflow.com
Ad