ytptube/app/library/Singleton.py
2025-01-28 19:43:51 +03:00

32 lines
856 B
Python

import threading
from typing import Any
class Singleton(type):
"""
A metaclass that creates a Singleton base class when called.
"""
_instances: dict[type, Any] = {}
def __call__(cls, *args: Any, **kwargs: Any) -> Any:
if cls not in cls._instances:
instance = super().__call__(*args, **kwargs)
cls._instances[cls] = instance
return cls._instances[cls]
class ThreadSafe(type):
"""
A metaclass that creates a Singleton base class when called.
"""
_instances: dict[type, Any] = {}
_lock = threading.Lock()
def __call__(cls, *args: Any, **kwargs: Any) -> Any:
with cls._lock:
if cls not in cls._instances:
instance = super().__call__(*args, **kwargs)
cls._instances[cls] = instance
return cls._instances[cls]