首页 > 编程语言 > 详细

python 单例模式

时间:2021-09-05 21:34:51      阅读:21      评论:0      收藏:0      [点我收藏+]

python 单例模式

import threading
import time
class Singleton(object):
    _instance_lock = threading.Lock()
    init=False
    def __init__(self,a):
        if Singleton.init==True:
            return
        Singleton.init=True
        print(a)
        pass
    def __new__(cls, *args, **kwargs):
        if not hasattr(Singleton, "_instance"):
            with Singleton._instance_lock:
                if not hasattr(Singleton, "_instance"):
                    Singleton._instance = object.__new__(cls)  
        return Singleton._instance

import threading

def task(arg):
    obj = Singleton(arg)
    print(obj)

for i in range(10):
    t = threading.Thread(target=task,args=[i,])
    t.start()

 

python 单例模式

原文:https://www.cnblogs.com/yan-2010/p/15226593.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!