如何在 while 循环中更新配置文件并同时从不同脚本获取值?

How to update Config File on while loop and get value from different script at same time?

这是场景,我有两个脚本可以说 abc.py 和 xyz.py

使用 abc.py 我想每隔一秒更新一次配置文件。这是示例代码。

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)

在 Xyz.py 我想从 config.ini 获取值。 我的代码在 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)

但问题是,ABC.py只更新了一次配置文件!这意味着它仅在 First While 循环中更新文件。它并没有像我想象的那样每秒更新配置文件。

ABC.py脚本中,Value不是在循环中生成的,因此,每一秒你都会在你的配置文件中写入相同的值。所以你的第二个脚本读取相同的值是正常的。