python 單例模式

來源:互聯網
上載者:User

標籤:python單例模式

python的單例模式實現:    重寫類的__new__方法即可    參照官網的解釋:        3.4.1. Basic customization                object.__new__(cls[, ...])                Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of __new__() should be the new object instance (usually an instance of cls).                Typical implementations create a new instance of the class by invoking the superclass’s __new__() method using super(currentclass, cls).__new__(cls[, ...]) with appropriate arguments and then modifying the newly-created instance as necessary before returning it.                If __new__() returns an instance of cls, then the new instance’s __init__() method will be invoked like __init__(self[,...]), where self is the new instance and the remaining arguments are the same as were passed to __new__().                If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.                __new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation.class db(object):    __instance = None    def __init__(self):        self.host = ‘10.0.0.1‘        self.port = ‘3306‘        self.user = ‘root‘        self.password = ‘123456‘    def __new__(cls,*args,**kwargs):       if cls.__instance is  None:           cls.__instance = super(db,cls).__new__(cls,*args,**kwargs)       return cls.__instance              In [2]: a=db()In [3]: b=db()In [4]: c=db()In [5]: aOut[5]: <__main__.db at 0x1066fd390>In [6]: bOut[6]: <__main__.db at 0x1066fd390>In [7]: cOut[7]: <__main__.db at 0x1066fd390>In [8]: a.hostOut[8]: ‘10.0.0.1‘In [9]: b.portOut[9]: ‘3306‘In [10]: c.userOut[10]: ‘root‘In [11]: c.password=‘uiso‘In [12]: b.passwordOut[12]: ‘uiso‘In [13]: a.passwordOut[13]: ‘uiso‘

可以看到, 初始化&執行個體化所使用的都是同一個對象,用來長串連的保持會非常好. 但是這種寫法不是安全執行緒的。


在多線程下,我們需要匯入使用threading的特性,使用鎖來保證安全執行緒


Lock = threading.Lock()


將__new__方法重寫為:

def __new__(cls, *args, **kwargs):        if not cls.__instance:            try:                Lock.acquire()                if not cls.__instance:                    cls.__instance = super(db, cls).__new__(cls, *args, **kwargs)            finally:                Lock.release()        return cls.__instance

double check來保證線程的安全。 

python 單例模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.