python 單例模式

來源:互聯網
上載者:User

標籤:問題   有一個   串連   因此   執行個體化   reading   password   span   log   

單例模式

單例模式(Singleton Pattern)是一種常用的軟體設計模式,該模式的主要目的是確保某一個類只有一個執行個體存在。當你希望在整個系統中,某個類只能出現一個執行個體時,單例對象就能派上用場。

比如資料庫連接讀取設定檔,如果在程式運行期間,有很多地方都需要串連資料庫,很多地方都需要建立資料庫物件的執行個體,這就導致系統中存在多個 資料庫執行個體對象,而這樣會嚴重浪費記憶體資源,事實上,我們希望在程式運行期間只存在一個執行個體對象。

實現單例模式的5種方式模組方式

其實,Python 的模組就是天然的單例模式,因為模組在第一次匯入時,會產生 .pyc 檔案,當第二次匯入時,就會直接載入 .pyc 檔案,而不會再次執行模組代碼。

因此,我們只需把相關的函數和資料定義在一個模組中,就可以獲得一個單例對象了。如果我們真的想要一個單例類,可以考慮這樣做:

mysingleton.py

class Singleton(object):    def foo(self):        passsingleton = Singleton()

 將上面的代碼儲存在檔案 mysingleton.py 中,要使用時,直接在其他檔案中匯入此檔案中的對象,這個對象即是單例模式的對象

from demo.my_singleton import singlesingle.foo()

  

使用裝飾器
def Singleton(cls):    _instance = {}    def _singleton(*args, **kargs):        if cls not in _instance:            _instance[cls] = cls(*args, **kargs)        return _instance[cls]    return _singleton@Singletonclass A(object):    def __init__(self, x=0):        self.x = xa1 = A(2)a2 = A(3)print(id(a1), id(a2))

  

使用類
class Singleton(object):    __instance = None    def __init__(self, x):        self.x = x        print(x)    @classmethod    def my_singleton(cls, *args, **kwargs):        if not cls.__instance:            cls.__instance = cls(*args, **kwargs)        return cls.__instance

如果遇到多線程會出現問題 

import threadingclass Singleton(object):    __instance = None    def __init__(self, x):        self.x = x        import time        time.sleep(1)  # 加入幹擾元素,造成多線程出現問題    @classmethod    def my_singleton(cls, *args, **kwargs):        if not cls.__instance:            cls.__instance = cls(*args, **kwargs)        return cls.__instanceimport threadingdef task(arg):    obj = Singleton.my_singleton(arg)    print(obj)for i in range(10):    t = threading.Thread(target=task, args=(i,))    t.start()-------------------<__main__.Singleton object at 0x00000000025D76D8><__main__.Singleton object at 0x000000000259FF28><__main__.Singleton object at 0x000000001234A208><__main__.Singleton object at 0x000000001234A1D0><__main__.Singleton object at 0x000000001234A438><__main__.Singleton object at 0x000000001234A630><__main__.Singleton object at 0x000000001234A828><__main__.Singleton object at 0x000000001234A978><__main__.Singleton object at 0x000000001234A748><__main__.Singleton object at 0x000000001234AAC8>

  

解決方案:加鎖!未加鎖部分並發執行,加鎖部分串列執行,速度降低,但是保證了資料安全

import threadingclass Singleton(object):    __instance = None    __instance_lock = threading.Lock()    def __init__(self, x):        self.x = x        import time        time.sleep(1)  # 加入幹擾元素,造成多線程出現問題    @classmethod    def my_singleton(cls, *args, **kwargs):        with cls.__instance_lock:  # 加鎖            if not cls.__instance:                cls.__instance = cls(*args, **kwargs)        return cls.__instanceimport threadingdef task(arg):    obj = Singleton.my_singleton(arg)    print(obj)for i in range(10):    t = threading.Thread(target=task, args=(i,))    t.start()----------------------<__main__.Singleton object at 0x000000000259FF28><__main__.Singleton object at 0x000000000259FF28><__main__.Singleton object at 0x000000000259FF28><__main__.Singleton object at 0x000000000259FF28><__main__.Singleton object at 0x000000000259FF28><__main__.Singleton object at 0x000000000259FF28><__main__.Singleton object at 0x000000000259FF28><__main__.Singleton object at 0x000000000259FF28><__main__.Singleton object at 0x000000000259FF28><__main__.Singleton object at 0x000000000259FF28>

  

基於__new__方法實現(推薦使用)

當我們執行個體化一個對象時,是先執行了類的__new__方法(我們沒寫時,預設調用object.__new__),執行個體化對象;然後再執行類的__init__方法,對這個對象進行初始化。

import threadingclass Singleton(object):    _instance_lock = threading.Lock()    def __init__(self, x):        self.x = x        import time        time.sleep(1)  # 加入幹擾元素,造成多線程出現問題    def __new__(cls, *args, **kwargs):        if not hasattr(cls, ‘_instance‘):            with cls._instance_lock:  # 加鎖                cls._instance = super(Singleton, cls).__new__(cls)        return cls._instanceimport threadingdef task(arg):    obj = Singleton(arg)    print(obj)for i in range(10):    t = threading.Thread(target=task, args=(i,))    t.start()----------------<__main__.Singleton object at 0x000000000257FF60><__main__.Singleton object at 0x000000000257FF60><__main__.Singleton object at 0x000000000257FF60><__main__.Singleton object at 0x000000000257FF60><__main__.Singleton object at 0x000000000257FF60><__main__.Singleton object at 0x000000000257FF60><__main__.Singleton object at 0x000000000257FF60><__main__.Singleton object at 0x000000000257FF60><__main__.Singleton object at 0x000000000257FF60><__main__.Singleton object at 0x000000000257FF60>

  

基於metaclass方式實現
1.類由type建立,建立類時,type的__init__方法自動執行,類() 執行type的 __call__方法(類的__new__方法,類的__init__方法)2.對象由類建立,建立對象時,類的__init__方法自動執行,對象()執行類的 __call__ 方法

 實現單例

import threadingclass SingletonType(type):    _instance_lock = threading.Lock()    def __init__(self,class_name,class_bases,class_dic):        super(SingletonType, self).__init__(class_name,class_bases,class_dic)    def __call__(cls, *args, **kwargs):        if not hasattr(cls, ‘_instance‘):            with cls._instance_lock:  # 加鎖                cls._instance = super(SingletonType, cls).__call__(*args, **kwargs)        return cls._instanceclass my_singlton(metaclass=SingletonType):    def __init__(self,x):        self.x = ximport threadingdef task(arg):    obj = my_singlton(arg)    print(obj)for i in range(10):    t = threading.Thread(target=task, args=(i,))    t.start()--------------------------<__main__.my_singlton object at 0x00000000025CFF60><__main__.my_singlton object at 0x00000000025CFF60><__main__.my_singlton object at 0x00000000025CFF60><__main__.my_singlton object at 0x00000000025CFF60><__main__.my_singlton object at 0x00000000025CFF60><__main__.my_singlton object at 0x00000000025CFF60><__main__.my_singlton object at 0x00000000025CFF60><__main__.my_singlton object at 0x00000000025CFF60><__main__.my_singlton object at 0x00000000025CFF60><__main__.my_singlton object at 0x00000000025CFF60>

  

單例模式使用

 

import threadingclass SingletonDB(object):    _instance_lock = threading.Lock()    def __init__(self,host=‘127.0.0.1‘,            port=3306,            user=‘root‘,            password=‘root‘,            database=‘testdb‘,            charset=‘utf8‘):        self.host = host        self.port = port        self.password = password        self.user = user        self.database = database        self.charset = charset    def __new__(cls, *args, **kwargs):        if not hasattr(SingletonDB, "_instance"):            with SingletonDB._instance_lock:                if not hasattr(SingletonDB, "_instance"):                    SingletonDB._instance = object.__new__(cls, *args, **kwargs)        return SingletonDB._instance    def connect(self):        print(‘connect db‘)db1 = SingletonDB()db2 = SingletonDB()print(db1,db2)db1.connect()db2.connect()----------------<__main__.SingletonDB object at 0x00000000025E76D8> <__main__.SingletonDB object at 0x00000000025E76D8>connect dbconnect db

  

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.