forked from shibing624/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_thread_demo.py
More file actions
38 lines (28 loc) · 927 Bytes
/
file_thread_demo.py
File metadata and controls
38 lines (28 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import json
import threading
mutex = threading.Lock()
def writetoTxt(json_file, id):
mutex.acquire()
data = load_json(json_file)
print('data', data, type(data))
data.update({id: {"username": str(id), "sex": 'man'}})
save_json(data, json_file)
mutex.release()
print('new data', data, type(data))
def load_json(json_file):
with open(json_file, 'r') as f:
data = json.load(f)
return data
def save_json(data, json_file):
with open(json_file, 'w') as f:
json.dump(data, f, indent=4)
if __name__ == '__main__':
a = {
"lili": {"username": "lili_01", "sex": "woman", "age": 12, "address": "beijing,china."},
"lucy": {"username": "lucy_1", "sex": "woman", "age": 32}
}
json_file = "a.json"
save_json(a, json_file)
for i in range(5):
myThread = threading.Thread(target=writetoTxt, args=(json_file,i,))
myThread.start()