新聞中心
簡單而言,單例模式就是保證某個(gè)實(shí)例在項(xiàng)目的整個(gè)生命周期中只存在一個(gè),在項(xiàng)目的任意位置使用,都是同一個(gè)實(shí)例。

十載專注成都網(wǎng)站制作,成都企業(yè)網(wǎng)站建設(shè),個(gè)人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識(shí)、方案,網(wǎng)站設(shè)計(jì)流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),專注于成都企業(yè)網(wǎng)站建設(shè),高端網(wǎng)頁制作,對(duì)成都攪拌罐車等多個(gè)領(lǐng)域,擁有多年的網(wǎng)站維護(hù)經(jīng)驗(yàn)。
單例模式雖然簡單,但還是有些門道的,而少有人知道這些門道。
邊界情況
Python中實(shí)現(xiàn)單例模式的方法很多,我以前最常使用的應(yīng)該是下面這種寫法。
class Singleton(object): _instance = None def __new__(cls, *args, **kw): if cls._instance is None: cls._instance = object.__new__(cls, *args, **kw) return cls._instance
這種寫法有兩個(gè)問題。
1.單例模式對(duì)應(yīng)類實(shí)例化時(shí)無法傳入?yún)?shù),將上面的代碼擴(kuò)展成下面形式。
class Singleton(object): _instance = None def __new__(cls, *args, **kw): if cls._instance is None: cls._instance = object.__new__(cls, *args, **kw) return cls._instance def __init(self, x, y): self.x = x self.y = y s = Singleton(1,2)
此時(shí)會(huì)拋出TypeError: object.__new__() takes exactly one argument (the type to instantiate)錯(cuò)誤
2.多個(gè)線程實(shí)例化Singleton類時(shí),可能會(huì)出現(xiàn)創(chuàng)建多個(gè)實(shí)例的情況,因?yàn)楹苡锌赡芏鄠€(gè)線程同時(shí)判斷cls._instance is None,從而進(jìn)入初
始化實(shí)例的代碼中。
基于同步鎖實(shí)現(xiàn)單例
先考慮上述實(shí)現(xiàn)遇到的第二個(gè)問題。
既然多線程情況下會(huì)出現(xiàn)邊界情況從而參數(shù)多個(gè)實(shí)例,那么使用同步鎖解決多線程的沖突則可。
import threading # 同步鎖 def synchronous_lock(func): def wrapper(*args, **kwargs): with threading.Lock(): return func(*args, **kwargs) return wrapper class Singleton(object): instance = None @synchronous_lock def __new__(cls, *args, **kwargs): if cls.instance is None: cls.instance = object.__new__(cls, *args, **kwargs) return cls.instance
上述代碼中通過threading.Lock()將單例化方法同步化,這樣在面對(duì)多個(gè)線程時(shí)也不會(huì)出現(xiàn)創(chuàng)建多個(gè)實(shí)例的情況,可以簡單試驗(yàn)一下。
def worker(): s = Singleton() print(id(s)) def test(): task = [] for i in range(10): t = threading.Thread(target=worker) task.append(t) for i in task: i.start() for i in task: i.join() test()
運(yùn)行后,打印的單例的id都是相同的。
更優(yōu)的方法
加了同步鎖之后,除了無法傳入?yún)?shù)外,已經(jīng)沒有什么大問題了,但是否有更優(yōu)的解決方法呢?單例模式是否有可以接受參數(shù)的實(shí)現(xiàn)方式?
def singleton(cls):
cls.__new_original__ = cls.__new__
@functools.wraps(cls.__new__)
def singleton_new(cls, *args, **kwargs):
it = cls.__dict__.get('__it__')
if it is not None:
return it
cls.__it__ = it = cls.__new_original__(cls, *args, **kwargs)
it.__init_original__(*args, **kwargs)
return it
cls.__new__ = singleton_new
cls.__init_original__ = cls.__init__
cls.__init__ = object.__init__
return cls
@singleton
class Foo(object):
def __new__(cls, *args, **kwargs):
cls.x = 10
return object.__new__(cls)
def __init__(self, x, y):
assert self.x == 10
self.x = x
self.y = y上述代碼中定義了singleton類裝飾器,裝飾器在預(yù)編譯時(shí)就會(huì)執(zhí)行,利用這個(gè)特性,singleton類裝飾器中替換了類原本的__new__與
__init__方法,使用singleton_new方法進(jìn)行類的實(shí)例化,在singleton_new方法中,先判斷類的屬性中是否存在__it__屬性,以此來判斷
是否要?jiǎng)?chuàng)建新的實(shí)例,如果要?jiǎng)?chuàng)建,則調(diào)用類原本的__new__方法完成實(shí)例化并調(diào)用原本的__init__方法將參數(shù)傳遞給當(dāng)前類,從而完成單
例模式的目的。
這種方法讓單例類可以接受對(duì)應(yīng)的參數(shù)但面對(duì)多線程同時(shí)實(shí)例化還是可能會(huì)出現(xiàn)多個(gè)實(shí)例,此時(shí)加上線程同步鎖則可。
def singleton(cls):
cls.__new_original__ = cls.__new__
@functools.wraps(cls.__new__)
def singleton_new(cls, *args, **kwargs):
# 同步鎖
with threading.Lock():
it = cls.__dict__.get('__it__')
if it is not None:
return it
cls.__it__ = it = cls.__new_original__(cls, *args, **kwargs)
it.__init_original__(*args, **kwargs)
return it
cls.__new__ = singleton_new
cls.__init_original__ = cls.__init__
cls.__init__ = object.__init__
return cls是否加同步鎖的額外考慮
如果一個(gè)項(xiàng)目不需要使用線程相關(guān)機(jī)制,只是在單例化這里使用了線程鎖,這其實(shí)不是必要的,它會(huì)拖慢項(xiàng)目的運(yùn)行速度。
閱讀CPython線程模塊相關(guān)的源碼,你會(huì)發(fā)現(xiàn),Python一開始時(shí)并沒有初始化線程相關(guān)的環(huán)境,只有當(dāng)你使用theading庫相關(guān)功能時(shí),
才會(huì)調(diào)用PyEval_InitThreads方法初始化多線程相關(guān)的環(huán)境,代碼片段如下(我省略了很多不相關(guān)代碼)。
static PyObject *
thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
{
PyObject *func, *args, *keyw = NULL;
struct bootstate *boot;
unsigned long ident;
// 初始化多線程環(huán)境,解釋器默認(rèn)不初始化,只有用戶使用時(shí),才初始化。
PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
// 創(chuàng)建線程
ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);
// 返回線程id
return PyLong_FromUnsignedLong(ident);
}為什么會(huì)這樣?
因?yàn)槎嗑€程環(huán)境會(huì)啟動(dòng)GIL鎖相關(guān)的邏輯,這會(huì)影響Python程序運(yùn)行速度。很多簡單的Python程序并不需要使用多線程,此時(shí)不需要初始化線程相關(guān)的環(huán)境,Python程序在沒有GIL鎖的情況下會(huì)運(yùn)行的更快。
如果你的項(xiàng)目中不會(huì)涉及多線程操作,那么就沒有使用有同步鎖來實(shí)現(xiàn)單例模式。
結(jié)尾
互聯(lián)網(wǎng)中有很多Python實(shí)現(xiàn)單例模式的文章,你只需要從多線程下是否可以保證單實(shí)例以及單例化時(shí)是否可以傳入初始參數(shù)兩點(diǎn)來判斷
相應(yīng)的實(shí)現(xiàn)方法則可。
標(biāo)題名稱:創(chuàng)新互聯(lián)Python教程:Python中的單例模式
網(wǎng)頁網(wǎng)址:http://fisionsoft.com.cn/article/ccodsdg.html


咨詢
建站咨詢
