python 单例

 

 

import threading

class ThreadSafeSingleton(type):
  _instances= {}
  _singleton_lock =threading.Lock()

def __call__(cls, *args, **kwargs):
  if cls not in cls._instances:
    with cls._singleton_lock:
      if cls not in cls._instances:
        cls._instances[cls] = super(ThreadSafeSingleton,cls).__call__(*args, **kwargs)
  return cls._instances[cls]

class M2TWConfig(metaclass=ThreadSafeSingleton):

  def __init__(self, *args, **kwargs):

    pass

 

posted @ 2023-02-12 12:05  是绵羊在漫步  阅读(16)  评论(0编辑  收藏  举报