资源简介
python 单例模式(入门级)
代码片段和文件信息
#!/usr/bin/python
# -*- coding: utf-8 -*-
import threading
def synchronized(func):
func.__lock__=threading.Lock()
def synced_func(*args**kws):
with func.__lock__ :
return func(*args**kws)
return synced_func
class Singleton(object):
instance=None
@synchronized
def __new__(cls*args**kw):
if cls.instance is None:
cls.instance = object.__new__(cls *args **kw)
return cls.instanc
评论
共有 条评论