Reasons and solutions for configopj unable to synchronize

There was a problem today: the system configuration file could not be synchronized.
The key value can be written, but the key value table data remains unchanged.

Problem description;
I wrote a main program, and then several sub-processes.
Then I want to modify a key value with a child process.
The display has been modified successfully, but the system primary key value is not synchronized.

I think: When the system calls the parameters of the child process, if another process calls the file, it will give the other process a copy of the key value. When the child process modifies the key value, it will not be synchronized in the main process.
When the file is not executed, the copy will be destroyed.
So the key value cannot be updated automatically.

Write a pseudo code:

`if 文件未打开:
        提供源文件
#修改内容
if  文件已被占用
      提供只读副本
#进程结束
      删除只读副本

The key value you modified is saved in a read-only copy, and then destroyed.
It took 3 hours...

The program body indicates:
if there is a key value try_1

import configobj
import threading
config = configobj.ConfigObj('F:\\d1\\passwd.ini')
def get1111():
        config['mail']['try_1'] = 1
        print(config['mail']['try_1'])


t3 = threading.Thread(target=get1111)
t3.setDaemon(True)
t3.start()

Open a program, and then there can be echo 1
Insert picture description here

Open dual processes, one of which occupies ini.

import configobj
import threading
config = configobj.ConfigObj('F:\\d1\\passwd.ini')
def loop1():
    # 这个进程占着键值啥都不做
    config['mail']['try_1']


def get1111():
        config['mail']['try_1'] = 1
        print("系统键值为")
        print(config['mail']['try_1'])

t4 = threading.Thread(target=loop1)
t4.setDaemon(True)
t4.start()
t3 = threading.Thread(target=get1111)
t3.setDaemon(True)
t3.start()

pass

In the picture above, there is a process occupying ini to
execute the program
Insert picture description here

Then there can be an echo of 1, indicating that the key value has been modified, and the child process key value is normal,
but the system key value is still 0. The
child process uses the key value that has been modified, while the main process uses the unmodified key value.

There seems to be such a logic:
each file has a key value table, only one program can modify the key value, and the others can only obtain read-only copies.
When the program calls the system, the first process can be modified, but the second one can’t...
What is the logic, there is a tutorial
http://www.voidspace.org.uk/python/configobj.html
but I am a small IJ, I I don’t want to read English... The translation quality is poor...
Waiting for time...

To deal with this problem: treat the key value as a file that cannot be modified, and manually modify the key value.
If you need variables, take out your old line:
write a file path for the key value, and write it in the file.

2333

Guess you like

Origin blog.csdn.net/weixin_45642669/article/details/113976389