Python之单例情势计算,Python之方式计算

1、单例方式

单例方式,方式

壹、单例形式

Python之单例情势计算,Python之方式计算。   
 单例形式分为各样:一.文件,2.类,3.依据__new__方法实现单例格局,4.基于metaclass形式完结

2.类完成如下:

亚洲必赢官网 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()

类达成的-单例形式不或者支撑二十多线程的气象
亚洲必赢官网 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)

类达成的-单例方式不能支撑二十四线程的意况

 

3.基于__new__艺术达成单例方式

亚洲必赢官网 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__主意落成的单例情势

 

4.基于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()
"""

 

 

 

 

亚洲必赢官网 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.文书,贰.类,三.基于__new__艺术完成单例格局,四.基于metaclass格局实现2.类完毕如下:…

一、单例情势

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、基于__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)

 

1、单例形式a、单例形式分为二种:文件,类,基于__new__办法完毕单例情势,基于metaclass形式落成…

   
a、单例形式分为多样:文件,类,基于__new__办法实现单例格局,基于metaclass情势达成

   
 单例情势分为三种:1.文本,二.类,三.基于__亚洲必赢官网 ,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()

亚洲必赢官网 5亚洲必赢官网 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()

类实现的-单例形式不大概支撑102线程的图景

    d、依照metaclass情势实现单例情势

亚洲必赢官网 7亚洲必赢官网 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)

 

类达成的-单例形式无法支撑拾贰线程的事态

 

3.基于__new__方法达成单例情势

亚洲必赢官网 9亚洲必赢官网 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()
"""

 

 

 

 

亚洲必赢官网 11亚洲必赢官网 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地图