1、单例情势

单例格局,形式

一、单例格局

   
 单例方式分为各类:1.文件,2.类,三.依据__new__措施达成单例格局,4.基于metaclass格局达成

二.类完毕如下:

金沙注册送58 1

class Sigletion(objects):
    import time
    def __init__(self):
        time.sleep(1)
    @classmethod
    def instance(cls,*args,**kwargs)
        if not hasattr(Sigletion,'_instance'):
            Sigletion._instance=Sigletion(*args,**kwargs)
        return Sigletion._instance

import threading

daf task(arg):
    obj=Sigletion.instance()
    print(obj)

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

类落成的-单例格局不或许支撑八线程的景色
金沙注册送58 2

import time
import threading
class Sigletion(object):
    _instance_lock=threading.Lock()
    def __init__(self):
        time.sleep(1)
    @classmethod
    def instance(cls,*args,**kwargs):
        if not hasattr(Sigletion,'_instance'):
            with Sigletion._instance_lock:
                if not hasattr(Sigletion,"_instace"):
                    Sigletion._instance=Sigletion(*args,**kwargs)
        return Sigletion._instance

def task(arg):
    obj=Sigletion.instance()
    print(obj)

for i in range(10):
    t=threading.Thread(target=task,args=[i,])
    t.start()
time.sleep(20)
obj=Sigletion.instance()
print(obj)

类完毕的-单例形式不能支撑二十八线程的情状

 

单例形式,Python之单例格局总括。3.基于__new__方法落成单例形式

金沙注册送58 3

import time
import threading
class Singleton(object):
   _instance_lock=threading.Lock()
   def __init__(self):
       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,*args,**kwargs)
       return Singleton._instance


obj1=Singleton()
obj2=Singleton()
print(obj1,obj2)

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

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

基于__new__方法达成的单例形式

 

四.基于metaclass情势达成单例格局

 

"""
1.对象是类创建,创建对象时候类的__init__方法自动执行,对象()执行类的 __call__ 方法
2.类是type创建,创建类时候type的__init__方法自动执行,类() 执行type的 __call__方法(类的__new__方法,类的__init__方法)

# 第0步: 执行type的 __init__ 方法【类是type的对象】
class Foo:
    def __init__(self):
        pass

    def __call__(self, *args, **kwargs):
        pass

# 第1步: 执行type的 __call__ 方法
#        1.1  调用 Foo类(是type的对象)的 __new__方法,用于创建对象。
#        1.2  调用 Foo类(是type的对象)的 __init__方法,用于对对象初始化。
obj = Foo()
# 第2步:执行Foodef __call__ 方法
obj()
"""

 

 

 

 

金沙注册送58 4

import threading

class SingletonType(type):
    _instace_lock=threading.Lock()
    def __call__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            with SingletonType._instace_lock:
                if not hasattr(cls, "_instance"):
                    cls._instance = super(SingletonType,cls).__call__(*args, **kwargs)
        return cls._instance
class Foo(metaclass=SingletonType):
    def __init__(self,name):
        self.name=name


obj1 = Foo('name')
obj2 = Foo('name')
print(obj1,obj2)

据书上说metaclass方式落成单例情势

 

1、单例模式单例格局分为三种:1.文件,贰.类,三.遵照__new__办法达成单例情势,四.基于metaclass格局达成二.类落成如下:…

一、单例情势

Python之单例格局计算,Python之方式总计

1、单例情势

   
a、单例方式分为种种:文件,类,基于__new__形式实现单例格局,基于metaclass格局落成

    b、类达成如下:

class Sigletion(objects):
    import time
    def __init__(self):
        time.sleep(1)
    @classmethod
    def instance(cls,*args,**kwargs)
        if not hasattr(Sigletion,'_instance'):
            Sigletion._instance=Sigletion(*args,**kwargs)
        return Sigletion._instance

import threading

daf task(arg):
    obj=Sigletion.instance()
    print(obj)

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

    c、基于__金沙注册送58 ,new__艺术完结单例情势

import time
import threading
class Singleton(object):
   _instance_lock=threading.Lock()
   def __init__(self):
       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,*args,**kwargs)
       return Singleton._instance


obj1=Singleton()
obj2=Singleton()
print(obj1,obj2)

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

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

    d、依照metaclass方式达成单例形式

"""
1.对象是类创建,创建对象时候类的__init__方法自动执行,对象()执行类的 __call__ 方法
2.类是type创建,创建类时候type的__init__方法自动执行,类() 执行type的 __call__方法(类的__new__方法,类的__init__方法)

# 第0步: 执行type的 __init__ 方法【类是type的对象】
class Foo:
    def __init__(self):
        pass

    def __call__(self, *args, **kwargs):
        pass

# 第1步: 执行type的 __call__ 方法
#        1.1  调用 Foo类(是type的对象)的 __new__方法,用于创建对象。
#        1.2  调用 Foo类(是type的对象)的 __init__方法,用于对对象初始化。
obj = Foo()
# 第2步:执行Foodef __call__ 方法
obj()
"""

import threading

class SingletonType(type):
    _instace_lock=threading.Lock()
    def __call__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            with SingletonType._instace_lock:
                if not hasattr(cls, "_instance"):
                    cls._instance = super(SingletonType,cls).__call__(*args, **kwargs)
        return cls._instance
class Foo(metaclass=SingletonType):
    def __init__(self,name):
        self.name=name


obj1 = Foo('name')
obj2 = Foo('name')
print(obj1,obj2)

 

壹、单例情势a、单例形式分为七种:文件,类,基于__new__方法达成单例情势,基于metaclass情势达成…

   
a、单例情势分为各种:文件,类,基于__new__方式完结单例情势,基于metaclass情势实现

   
 单例格局分为三种:一.文件,贰.类,3.基于__new__格局达成单例方式,4.基于metaclass格局完毕

    b、类落成如下:

二.类实现如下:

class Sigletion(objects):
    import time
    def __init__(self):
        time.sleep(1)
    @classmethod
    def instance(cls,*args,**kwargs)
        if not hasattr(Sigletion,'_instance'):
            Sigletion._instance=Sigletion(*args,**kwargs)
        return Sigletion._instance

import threading

daf task(arg):
    obj=Sigletion.instance()
    print(obj)

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

金沙注册送58 5金沙注册送58 6

    c、基于__new__方法达成单例情势

class Sigletion(objects):
    import time
    def __init__(self):
        time.sleep(1)
    @classmethod
    def instance(cls,*args,**kwargs)
        if not hasattr(Sigletion,'_instance'):
            Sigletion._instance=Sigletion(*args,**kwargs)
        return Sigletion._instance

import threading

daf task(arg):
    obj=Sigletion.instance()
    print(obj)

for i in range(10):
    t=threading.Thread(target=task,args=[i,])
    t.start()
import time
import threading
class Singleton(object):
   _instance_lock=threading.Lock()
   def __init__(self):
       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,*args,**kwargs)
       return Singleton._instance


obj1=Singleton()
obj2=Singleton()
print(obj1,obj2)

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

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

类落成的-单例格局无法支撑八线程的地方

    d、根据metaclass方式达成单例方式

金沙注册送58 7金沙注册送58 8

"""
1.对象是类创建,创建对象时候类的__init__方法自动执行,对象()执行类的 __call__ 方法
2.类是type创建,创建类时候type的__init__方法自动执行,类() 执行type的 __call__方法(类的__new__方法,类的__init__方法)

# 第0步: 执行type的 __init__ 方法【类是type的对象】
class Foo:
    def __init__(self):
        pass

    def __call__(self, *args, **kwargs):
        pass

# 第1步: 执行type的 __call__ 方法
#        1.1  调用 Foo类(是type的对象)的 __new__方法,用于创建对象。
#        1.2  调用 Foo类(是type的对象)的 __init__方法,用于对对象初始化。
obj = Foo()
# 第2步:执行Foodef __call__ 方法
obj()
"""

import threading

class SingletonType(type):
    _instace_lock=threading.Lock()
    def __call__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            with SingletonType._instace_lock:
                if not hasattr(cls, "_instance"):
                    cls._instance = super(SingletonType,cls).__call__(*args, **kwargs)
        return cls._instance
class Foo(metaclass=SingletonType):
    def __init__(self,name):
        self.name=name


obj1 = Foo('name')
obj2 = Foo('name')
print(obj1,obj2)
import time
import threading
class Sigletion(object):
    _instance_lock=threading.Lock()
    def __init__(self):
        time.sleep(1)
    @classmethod
    def instance(cls,*args,**kwargs):
        if not hasattr(Sigletion,'_instance'):
            with Sigletion._instance_lock:
                if not hasattr(Sigletion,"_instace"):
                    Sigletion._instance=Sigletion(*args,**kwargs)
        return Sigletion._instance

def task(arg):
    obj=Sigletion.instance()
    print(obj)

for i in range(10):
    t=threading.Thread(target=task,args=[i,])
    t.start()
time.sleep(20)
obj=Sigletion.instance()
print(obj)

 

类落成的-单例形式不能够支撑10二线程的情状

 

3.基于__new__措施实现单例方式

金沙注册送58 9金沙注册送58 10

import time
import threading
class Singleton(object):
   _instance_lock=threading.Lock()
   def __init__(self):
       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,*args,**kwargs)
       return Singleton._instance


obj1=Singleton()
obj2=Singleton()
print(obj1,obj2)

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

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

基于__new__方法完毕的单例形式

 

肆.基于metaclass格局落成单例方式

 

"""
1.对象是类创建,创建对象时候类的__init__方法自动执行,对象()执行类的 __call__ 方法
2.类是type创建,创建类时候type的__init__方法自动执行,类() 执行type的 __call__方法(类的__new__方法,类的__init__方法)

# 第0步: 执行type的 __init__ 方法【类是type的对象】
class Foo:
    def __init__(self):
        pass

    def __call__(self, *args, **kwargs):
        pass

# 第1步: 执行type的 __call__ 方法
#        1.1  调用 Foo类(是type的对象)的 __new__方法,用于创建对象。
#        1.2  调用 Foo类(是type的对象)的 __init__方法,用于对对象初始化。
obj = Foo()
# 第2步:执行Foodef __call__ 方法
obj()
"""

 

 

 

 

金沙注册送58 11金沙注册送58 12

import threading

class SingletonType(type):
    _instace_lock=threading.Lock()
    def __call__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            with SingletonType._instace_lock:
                if not hasattr(cls, "_instance"):
                    cls._instance = super(SingletonType,cls).__call__(*args, **kwargs)
        return cls._instance
class Foo(metaclass=SingletonType):
    def __init__(self,name):
        self.name=name


obj1 = Foo('name')
obj2 = Foo('name')
print(obj1,obj2)

依照metaclass格局实现单例格局

 

相关文章

网站地图xml地图