Merge pull request #1884 from thsrite/main

This commit is contained in:
jxxghp
2024-04-13 18:03:34 +08:00
committed by GitHub

View File

@ -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):