# -*- coding: utf-8 -*- import threading class Singleton(object): ''' 实现单件模式的基类, 好像就是从Python Cookbook扣来的吧ww ''' objs = {} objs_locker = threading.Lock() def __new__(cls, *args, **kv): if cls in cls.objs: return cls.objs[cls]['obj'] cls.objs_locker.acquire() try: if cls in cls.objs: ## double check locking return cls.objs[cls]['obj'] obj = object.__new__(cls) cls.objs[cls] = {'obj': obj, 'init': False} setattr(cls, '__init__', cls.decorate_init(cls.__init__)) finally: cls.objs_locker.release() return obj @classmethod def decorate_init(cls, fn): def init_wrap(*args): if not cls.objs[cls]['init']: fn(*args) cls.objs[cls]['init'] = True return return init_wrap
python 单例 decorate_init
最新推荐文章于 2022-09-18 21:41:23 发布