tornado singleton单例模式

来源:互联网 发布:APP登录HTML源码 编辑:程序博客网 时间:2024/04/30 01:26

class TOLoop:
@staticmethod
    def instance():
        """Returns a global `IOLoop` instance.
Most applications have a single, global `IOLoop` running on the
main thread. Use this method to get this instance from
another thread. To get the current thread's `IOLoop`, use `current()`.
"""
        if not hasattr(IOLoop, "_instance"): //避免无意义的锁定, 因为大部分调用时只是简单的返回已经创建的_instance
            with IOLoop._instance_lock:
                if not hasattr(IOLoop, "_instance"):
                    # New instance after double check
                    IOLoop._instance = IOLoop()
        return IOLoop._instance

Borg is good for SUBCLASS

class Borg:  __shared_state = {}  # init internal state variables here  __register = {}  def __init__(self):    self.__dict__ = self.__shared_state    if not self.__register:      self._init_default_register()

for example 

class SubBorg(Borg):
pass



0 0