Tornado源码分析1-Configurable
来源:互联网 发布:彩票关注系统源码 编辑:程序博客网 时间:2024/04/27 18:39
class Configurable(object): """ 从下面的注释可以看出Configurable为工厂函数的实现提供了便利,用户可以通过调用configure或者其他设置获取所需要的类实例。 """ """Base class for configurable interfaces. A configurable interface is an (abstract) class whose constructor acts as a factory function for one of its implementation subclasses. The implementation subclass as well as optional keyword arguments to its initializer can be set globally at runtime with `configure`. By using the constructor as the factory method, the interface looks like a normal class, `isinstance` works as usual, etc. This pattern is most useful when the choice of implementation is likely to be a global decision (e.g. when `~select.epoll` is available, always use it instead of `~select.select`), or when a previously-monolithic class has been split into specialized subclasses. Configurable subclasses must define the class methods `configurable_base` and `configurable_default`, and use the instance method `initialize` instead of ``__init__``. """ __impl_class = None # 具化且已实现的类 __impl_kwargs = None """这是一个用户可以调用的函数,cls需要是当前类A或其子类,在A.configure(cls)后,以后的A()会返回cls的实例""" @classmethod def configure(cls, impl, **kwargs): """Sets the class to use when the base class is instantiated. Keyword arguments will be saved and added to the arguments passed to the constructor. This can be used to set global defaults for some parameters. """ base = cls.configurable_base() if isinstance(impl, (unicode_type, bytes)): impl = import_object(impl) if impl is not None and not issubclass(impl, cls): raise ValueError("Invalid subclass of %s" % cls) # 指定__impl_class base.__impl_class = impl base.__impl_kwargs = kwargs """返回当前指定的具化cls""" @classmethod def configured_class(cls): """Returns the currently configured class.""" base = cls.configurable_base() if cls.__impl_class is None: base.__impl_class = cls.configurable_default() return base.__impl_class # 可以通过重载此方法来配置具化类的生成,参考IOLoop的函数 @classmethod def configurable_default(cls): pass def __new__(cls, *args, **kwargs): base = cls.configurable_base() init_kwargs = {} if cls is base: impl = cls.configured_class() if base.__impl_kwargs: init_kwargs.update(base.__impl_kwargs) else: impl = cls init_kwargs.update(kwargs) instance = super(Configurable, cls).__new__(impl) # initialize vs __init__ chosen for compatibility with AsyncHTTPClient # singleton magic. If we get rid of that we can switch to __init__ # here too. instance.initialize(*args, **init_kwargs) return instance
IOLoop的configurable_default实现
@classmethod def configurable_default(cls): #从当前的操作系统环境给出具体的IOLoop类 if hasattr(select, "epoll"): from tornado.platform.epoll import EPollIOLoop return EPollIOLoop if hasattr(select, "kqueue"): # Python 2.6+ on BSD or Mac from tornado.platform.kqueue import KQueueIOLoop return KQueueIOLoop from tornado.platform.select import SelectIOLoop return SelectIOLoop
0 0
- Tornado源码分析1-Configurable
- Tornado源码分析
- tornado源码分析
- TORNADO源码分析
- tornado源码分析-Application
- Tornado: 2. 源码分析 (3)
- Tornado源码分析之IOLoop
- Tornado源码分析之http服务器篇
- Tornado源码分析之http服务器篇
- Tornado源码分析之http服务器篇
- Tornado源码分析之http服务器篇
- Tornado源码分析之http服务器篇
- Tornado源码分析之http服务器篇
- Tornado源码分析之http服务器篇
- tornado之ioloop类源码分析
- Tornado源码分析之http服务器篇
- Tornado源码分析之http服务器篇
- 开始Tornado的源码分析之旅
- 2016年年中计划
- [dfs] BZOJ 4602 [Sdoi2016]齿轮
- seajs
- TCP 协议中MSS的理解
- 史上最强Sublime 笔记系列---精选插件和UI主题
- Tornado源码分析1-Configurable
- ios多线程 -- 线程安全
- SqlServer中BULK INSERT用法简介,批量插入数据
- Sliding Window Maximum
- java基础篇(六)方法重写
- 解决BitmapFactory.decodeResource时出现图片缩放的问题
- memmove与memcpy区别
- 基本的排序算法
- 字符串操作的函数