python 单例模式

装饰器单例模式

 

import threading
import time
class  SingLeton(object):
    _instance = None
    lock = threading.RLock()
    def __new__(cls):
        if cls._instance:
            return cls._instance
        with cls.lock:
            if not cls._instance:
                time.sleep(1)
                cls._instance = super(SingLeton,cls).__new__(cls)
            return cls._instance

def fun():
    obj=SingLeton()
    print(obj)

for i in range(10):
    t = threading.Thread(target=fun)
    t.start()
原文地址:https://www.cnblogs.com/wzy23/p/13489124.html