From b26ce25719f9478cbc02cd0f0ec7e63c50ec62d3 Mon Sep 17 00:00:00 2001 From: grillazz Date: Fri, 20 Dec 2024 18:06:21 +0100 Subject: [PATCH] add singleton meta with no args --- app/utils/singleton.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/utils/singleton.py b/app/utils/singleton.py index 0c94176..85df17e 100644 --- a/app/utils/singleton.py +++ b/app/utils/singleton.py @@ -16,3 +16,21 @@ class SingletonMeta(type): instance = super().__call__(*args, **kwargs) cls._instances[cls] = instance return cls._instances[cls] + + +class SingletonMetaNoArgs(type): + """ + Singleton metaclass for classes without parameters on constructor, + for compatibility with FastApi Depends() function. + """ + + _instances = {} + + _lock: Lock = Lock() + + def __call__(cls): + with cls._lock: + if cls not in cls._instances: + instance = super().__call__() + cls._instances[cls] = instance + return cls._instances[cls] \ No newline at end of file