From fc65cc361922348f1f94613f7c3cc81457b30099 Mon Sep 17 00:00:00 2001 From: thsrite Date: Sat, 13 Apr 2024 17:33:08 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E5=8D=95=E4=BE=8B=E5=8A=A0=E9=94=81?= =?UTF-8?q?=EF=BC=8C=E9=98=B2=E6=AD=A2init=E6=96=B9=E6=B3=95=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E8=BF=87=E9=95=BF=E5=AF=BC=E8=87=B4=E5=A4=9A=E6=AC=A1?= =?UTF-8?q?init?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/utils/singleton.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/utils/singleton.py b/app/utils/singleton.py index 908b2ffd..057531d6 100644 --- a/app/utils/singleton.py +++ b/app/utils/singleton.py @@ -1,4 +1,5 @@ import abc +import threading class Singleton(abc.ABCMeta, type): @@ -7,12 +8,14 @@ class Singleton(abc.ABCMeta, type): """ _instances: dict = {} + _lock = threading.Lock() def __call__(cls, *args, **kwargs): key = (cls, args, frozenset(kwargs.items())) - if key not in cls._instances: - cls._instances[key] = super().__call__(*args, **kwargs) - return cls._instances[key] + with cls._lock: + if key not in cls._instances: + cls._instances[key] = super().__call__(*args, **kwargs) + return cls._instances[key] class AbstractSingleton(abc.ABC, metaclass=Singleton):