Factory function that returns a new reentrant lock.
A reentrant lock must be released by the thread that acquired it. Once a thread has acquired a reentrant lock, the same thread may acquire it again without blocking; the thread must release it once for each time it has acquired it
可重入锁必须由获取它的线程释放。一旦一个线程获取了可重入锁定,同一个线程就可以再次获取它而不会阻塞;线程每次获取它时都必须释放它一次
# 使用如下:
import os
import json
import threading
from collections import OrderedDict
path = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
report_name = os.path.join(path, 'result/test_report.json')
lock = threading.RLock()
def refresh_report(**kwargs):
lock.acquire()
try:
with open(report_name, 'r') as f:
data = json.load(f)
data_now = data
if kwargs.get("component"):
comp = kwargs['component'] + "StressTest"
for k, v in kwargs.items():
if k == 'component':
continue
data_now[comp][k] = v
else:
for k, v in kwargs.items():
data_now["FinalResult"][k] = v
with open(report_name, 'w') as f:
json.dump(OrderedDict(data_now), f, indent=4, ensure_ascii=False)
except Exception as e:
print("write " + report_name + "error:" + str(e))
return False
finally:
lock.release()
return True