MoviePilot/app/utils/singleton.py

24 lines
527 B
Python

import abc
import threading
class Singleton(abc.ABCMeta, type):
"""
类单例模式
"""
_instances: dict = {}
_lock = threading.Lock()
def __call__(cls, *args, **kwargs):
key = (cls, args, frozenset(kwargs.items()))
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):
"""
抽像类单例模式
"""