import time from threading import Lock from concurrent.futures import ThreadPoolExecutor class Test(): pass class Singleton(): _local = Lock() _insrance = None @classmethod def instance(cls, *args, **kwargs): if not cls._insrance: with cls._local: if not cls._insrance: time.sleep(1) cls._insrance = Test() return cls._insrance def get_instance(): print(id(Singleton.instance())) if __name__ == "__main__": poll = ThreadPoolExecutor(max_workers=5) for i in range(10): poll.submit(get_instance)
标签:__,创建,模式,insrance,instance,单例,._,import,cls From: https://www.cnblogs.com/fuchenjie/p/17508685.html