日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python中的單例模式的幾種實(shí)現(xiàn)方式的及優(yōu)化

系統(tǒng) 1759 0

單例模式

單例模式(Singleton Pattern)是一種常用的軟件設(shè)計(jì)模式,該模式的主要目的是確保某一個(gè)類只有一個(gè)實(shí)例存在。當(dāng)你希望在整個(gè)系統(tǒng)中,某個(gè)類只能出現(xiàn)一個(gè)實(shí)例時(shí),單例對象就能派上用場。

比如,某個(gè)服務(wù)器程序的配置信息存放在一個(gè)文件中,客戶端通過一個(gè) AppConfig 的類來讀取配置文件的信息。如果在程序運(yùn)行期間,有很多地方都需要使用配置文件的內(nèi)容,也就是說,很多地方都需要?jiǎng)?chuàng)建 AppConfig 對象的實(shí)例,這就導(dǎo)致系統(tǒng)中存在多個(gè) AppConfig 的實(shí)例對象,而這樣會嚴(yán)重浪費(fèi)內(nèi)存資源,尤其是在配置文件內(nèi)容很多的情況下。事實(shí)上,類似 AppConfig 這樣的類,我們希望在程序運(yùn)行期間只存在一個(gè)實(shí)例對象。

在 Python 中,我們可以用多種方法來實(shí)現(xiàn)單例模式

實(shí)現(xiàn)單例模式的幾種方式

1.使用模塊
其實(shí),Python 的模塊就是天然的單例模式,因?yàn)槟K在第一次導(dǎo)入時(shí),會生成 .pyc 文件,當(dāng)?shù)诙螌?dǎo)入時(shí),就會直接加載 .pyc 文件,而不會再次執(zhí)行模塊代碼。因此,我們只需把相關(guān)的函數(shù)和數(shù)據(jù)定義在一個(gè)模塊中,就可以獲得一個(gè)單例對象了。如果我們真的想要一個(gè)單例類,可以考慮這樣做:

mysingleton.py

          
            '''
遇到不懂的問題?Python學(xué)習(xí)交流群:821460695滿足你的需求,資料都已經(jīng)上傳群文件,可以自行下載!
'''
class Singleton(object):
    def foo(self):
        pass
singleton = Singleton()

          
        

將上面的代碼保存在文件 mysingleton.py 中,要使用時(shí),直接在其他文件中導(dǎo)入此文件中的對象,這個(gè)對象即是單例模式的對象

          
            from a import singleton

          
        

2.使用裝飾器

          
            def Singleton(cls):
    _instance = {}

    def _singleton(*args, **kargs):
        if cls not in _instance:
            _instance[cls] = cls(*args, **kargs)
        return _instance[cls]

    return _singleton


@Singleton
class A(object):
    a = 1

    def __init__(self, x=0):
        self.x = x


a1 = A(2)
a2 = A(3)

          
        

3.使用類

          
            class Singleton(object):

    def __init__(self):
        pass

    @classmethod
    def instance(cls, *args, **kwargs):
        if not hasattr(Singleton, "_instance"):
            Singleton._instance = Singleton(*args, **kwargs)
        return Singleton._instance

          
        

一般情況,大家以為這樣就完成了單例模式,但是這樣當(dāng)使用多線程時(shí)會存在問題

          
            class Singleton(object):

    def __init__(self):
        pass

    @classmethod
    def instance(cls, *args, **kwargs):
        if not hasattr(Singleton, "_instance"):
            Singleton._instance = Singleton(*args, **kwargs)
        return Singleton._instance

import threading

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

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

          
        

程序執(zhí)行后,打印結(jié)果如下:

          
            <__main__.Singleton object at 0x02C933D0>
<__main__.Singleton object at 0x02C933D0>
<__main__.Singleton object at 0x02C933D0>
<__main__.Singleton object at 0x02C933D0>
<__main__.Singleton object at 0x02C933D0>
<__main__.Singleton object at 0x02C933D0>
<__main__.Singleton object at 0x02C933D0>
<__main__.Singleton object at 0x02C933D0>
<__main__.Singleton object at 0x02C933D0>
<__main__.Singleton object at 0x02C933D0>

          
        

看起來也沒有問題,那是因?yàn)閳?zhí)行速度過快,如果在init方法中有一些IO操作,就會發(fā)現(xiàn)問題了,下面我們通過time.sleep模擬

我們在上面 init 方法中加入以下代碼:

          
                def __init__(self):
        import time
        time.sleep(1)

          
        

重新執(zhí)行程序后,結(jié)果如下

          
            <__main__.Singleton object at 0x034A3410>
<__main__.Singleton object at 0x034BB990>
<__main__.Singleton object at 0x034BB910>
<__main__.Singleton object at 0x034ADED0>
<__main__.Singleton object at 0x034E6BD0>
<__main__.Singleton object at 0x034E6C10>
<__main__.Singleton object at 0x034E6B90>
<__main__.Singleton object at 0x034BBA30>
<__main__.Singleton object at 0x034F6B90>
<__main__.Singleton object at 0x034E6A90>

          
        

問題出現(xiàn)了!按照以上方式創(chuàng)建的單例,無法支持多線程
解決辦法:加鎖!未加鎖部分并發(fā)執(zhí)行,加鎖部分串行執(zhí)行,速度降低,但是保證了數(shù)據(jù)安全

          
            import time
import threading
class Singleton(object):
    _instance_lock = threading.Lock()

    def __init__(self):
        time.sleep(1)

    @classmethod
    def instance(cls, *args, **kwargs):
        with Singleton._instance_lock:
            if not hasattr(Singleton, "_instance"):
                Singleton._instance = Singleton(*args, **kwargs)
        return Singleton._instance


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

          
        

打印結(jié)果如下:

          
            <__main__.Singleton object at 0x02D6B110>
<__main__.Singleton object at 0x02D6B110>
<__main__.Singleton object at 0x02D6B110>
<__main__.Singleton object at 0x02D6B110>
<__main__.Singleton object at 0x02D6B110>
<__main__.Singleton object at 0x02D6B110>
<__main__.Singleton object at 0x02D6B110>
<__main__.Singleton object at 0x02D6B110>
<__main__.Singleton object at 0x02D6B110>
<__main__.Singleton object at 0x02D6B110>

          
        

這樣就差不多了,但是還是有一點(diǎn)小問題,就是當(dāng)程序執(zhí)行時(shí),執(zhí)行了time.sleep(20)后,下面實(shí)例化對象時(shí),此時(shí)已經(jīng)是單例模式了,但我們還是加了鎖,這樣不太好,再進(jìn)行一些優(yōu)化,把intance方法,改成下面的這樣就行:

          
            @classmethod
    def instance(cls, *args, **kwargs):
        if not hasattr(Singleton, "_instance"):
            with Singleton._instance_lock:
                if not hasattr(Singleton, "_instance"):
                    Singleton._instance = Singleton(*args, **kwargs)
        return Singleton._instance

          
        

這樣,一個(gè)可以支持多線程的單例模式就完成了

完整代碼

          
            import time
import threading
class Singleton(object):
    _instance_lock = threading.Lock()

    def __init__(self):
        time.sleep(1)

    @classmethod
    def instance(cls, *args, **kwargs):
        if not hasattr(Singleton, "_instance"):
            with Singleton._instance_lock:
                if not hasattr(Singleton, "_instance"):
                    Singleton._instance = Singleton(*args, **kwargs)
        return Singleton._instance


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


          
        

這種方式實(shí)現(xiàn)的單例模式,使用時(shí)會有限制,以后實(shí)例化必須通過 obj = Singleton.instance()

如果用 obj=Singleton() ,這種方式得到的不是單例

4.基于 new 方法實(shí)現(xiàn)(推薦使用,方便)

通過上面例子,我們可以知道,當(dāng)我們實(shí)現(xiàn)單例時(shí),為了保證線程安全需要在內(nèi)部加入鎖

我們知道,當(dāng)我們實(shí)例化一個(gè)對象時(shí),是先執(zhí)行了類的 new 方法(我們沒寫時(shí),默認(rèn)調(diào)用object. new ),實(shí)例化對象;然后再執(zhí)行類的 init 方法,對這個(gè)對象進(jìn)行初始化,所有我們可以基于這個(gè),實(shí)現(xiàn)單例模式

          
            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)  
        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()

          
        

打印結(jié)果如下:

          
            <__main__.Singleton object at 0x038B33D0> <__main__.Singleton object at 0x038B33D0>
<__main__.Singleton object at 0x038B33D0>
<__main__.Singleton object at 0x038B33D0>
<__main__.Singleton object at 0x038B33D0>
<__main__.Singleton object at 0x038B33D0>
<__main__.Singleton object at 0x038B33D0>
<__main__.Singleton object at 0x038B33D0>
<__main__.Singleton object at 0x038B33D0>
<__main__.Singleton object at 0x038B33D0>
<__main__.Singleton object at 0x038B33D0>
<__main__.Singleton object at 0x038B33D0>

          
        

采用這種方式的單例模式,以后實(shí)例化對象時(shí),和平時(shí)實(shí)例化對象的方法一樣 obj = Singleton()

5.基于metaclass方式實(shí)現(xiàn)

相關(guān)知識

          
            """
1.類由type創(chuàng)建,創(chuàng)建類時(shí),type的__init__方法自動執(zhí)行,類() 執(zhí)行type的 __call__方法(類的__new__方法,類的__init__方法)
2.對象由類創(chuàng)建,創(chuàng)建對象時(shí),類的__init__方法自動執(zhí)行,對象()執(zhí)行類的 __call__ 方法
"""

          
        

例子:

          
            '''
遇到不懂的問題?Python學(xué)習(xí)交流群:821460695滿足你的需求,資料都已經(jīng)上傳群文件,可以自行下載!
'''
class Foo:
    def __init__(self):
        pass

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

obj = Foo()
# 執(zhí)行type的 __call__ 方法,調(diào)用 Foo類(是type的對象)的 __new__方法,用于創(chuàng)建對象,然后調(diào)用 Foo類(是type的對象)的 __init__方法,用于對對象初始化。

obj()    # 執(zhí)行Foo的 __call__ 方法

          
        

元類的使用

          
            class SingletonType(type):
    def __init__(self,*args,**kwargs):
        super(SingletonType,self).__init__(*args,**kwargs)

    def __call__(cls, *args, **kwargs): # 這里的cls,即Foo類
        print('cls',cls)
        obj = cls.__new__(cls,*args, **kwargs)
        cls.__init__(obj,*args, **kwargs) # Foo.__init__(obj)
        return obj

class Foo(metaclass=SingletonType): # 指定創(chuàng)建Foo的type為SingletonType
    def __init__(self,name):
        self.name = name
    def __new__(cls, *args, **kwargs):
        return object.__new__(cls)

obj = Foo('xx')

          
        

實(shí)現(xiàn)單例模式

          
            import threading

class SingletonType(type):
    _instance_lock = threading.Lock()
    def __call__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            with SingletonType._instance_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)

          
        

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 湖州市| 绩溪县| 大丰市| 长葛市| 双柏县| 九江市| 淮阳县| 和政县| 闵行区| 东源县| 宁明县| 喀什市| 渝北区| 德化县| 调兵山市| 志丹县| 婺源县| 商都县| 合川市| 高州市| 邯郸市| 黄山市| 夹江县| 唐海县| 郑州市| 台山市| 大新县| 宜黄县| 五莲县| 莱阳市| 九台市| 武清区| 和平区| 永嘉县| 台安县| 新平| 康马县| 舞钢市| 莎车县| 桐庐县| 尚义县|