Skip to content

Commit b2bf697

Browse files
authored
perf: Refine the Model Manager code (#3094)
1 parent 8cf66b9 commit b2bf697

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

apps/common/config/embedding_config.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,31 @@
1212
from common.cache.mem_cache import MemCache
1313

1414
_lock = threading.Lock()
15+
locks = {}
1516

1617

1718
class ModelManage:
1819
cache = MemCache('model', {})
1920
up_clear_time = time.time()
2021

22+
@staticmethod
23+
def _get_lock(_id):
24+
lock = locks.get(_id)
25+
if lock is None:
26+
with _lock:
27+
lock = locks.get(_id)
28+
if lock is None:
29+
lock = threading.Lock()
30+
locks[_id] = lock
31+
32+
return lock
33+
2134
@staticmethod
2235
def get_model(_id, get_model):
2336
model_instance = ModelManage.cache.get(_id)
2437
if model_instance is None:
25-
with _lock:
38+
lock = ModelManage._get_lock(_id)
39+
with lock:
2640
model_instance = ModelManage.cache.get(_id)
2741
if model_instance is None:
2842
model_instance = get_model(_id)

0 commit comments

Comments
 (0)