Skip to content

Commit b26ce25

Browse files
committed
add singleton meta with no args
1 parent 9d08ae9 commit b26ce25

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

app/utils/singleton.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,21 @@ def __call__(cls, *args, **kwargs):
1616
instance = super().__call__(*args, **kwargs)
1717
cls._instances[cls] = instance
1818
return cls._instances[cls]
19+
20+
21+
class SingletonMetaNoArgs(type):
22+
"""
23+
Singleton metaclass for classes without parameters on constructor,
24+
for compatibility with FastApi Depends() function.
25+
"""
26+
27+
_instances = {}
28+
29+
_lock: Lock = Lock()
30+
31+
def __call__(cls):
32+
with cls._lock:
33+
if cls not in cls._instances:
34+
instance = super().__call__()
35+
cls._instances[cls] = instance
36+
return cls._instances[cls]

0 commit comments

Comments
 (0)