IOLoop主要工作
1、将TCPServer 注册到 IOLoop 的事件记到 _handlers 字段,同时注册 READ 和 ERROR 事件到 epoll
2、IOLoop 启动一个大循环,负责轮询epoll中是否已经有就绪的事件,如果有就执行对应的回调
 
以下为源码分析,省略部分源码,只取主要部分
 1 class Configurable(object):
 2     """根据子类的配置,来创建一个对象,也就是说,继承自Configurable的子类,可以自己配置产生不同的类,并且每个类都会执行initialize方法
 3     __impl_class = None
 4     __impl_kwargs = None
 5 
 6     def __new__(cls, **kwargs):   
 7         base = cls.configurable_base()
 8         args = {}
 9         if cls is base:
10             impl = cls.configured_class()
11             if base.__impl_kwargs:
12                 args.update(base.__impl_kwargs)
13         else:
14             impl = cls
15         args.update(kwargs)
16         instance = super(Configurable, cls).__new__(impl)
17         instance.initialize(**args)   #执行initialize方法
18         return instance
Configurable
 1 class IOLoop(Configurable):
 2 " 一个大循环,自动根据当前的系统,是 linux 2.5以上选择epoll, mac 选择kqueue, 其他选择select".
 3 @staticmethod
 4 def instance():   #创建一个全局的 IOLoop 单例
 5     if not hasattr(IOLoop, "_instance"):
 6         with IOLoop._instance_lock:
 7             if not hasattr(IOLoop, "_instance"):
 8                 IOLoop._instance = IOLoop()
 9     return IOLoop._instance
10 
11 
12 @classmethod    配置
13 def configurable_base(cls):
14     return IOLoop
15 
16 @classmethod
17 def configurable_default(cls):   #根据系统,配置使用epoll还是 kqueue
18     if hasattr(select, "epoll"):
19         from tornado.platform.epoll import EPollIOLoop
20         return EPollIOLoop
21     if hasattr(select, "kqueue"):
22         # Python 2.6+ on BSD or Mac
23         from tornado.platform.kqueue import KQueueIOLoop
24         return KQueueIOLoop
25     from tornado.platform.select import SelectIOLoop
26     return SelectIOLoop
IOLoop

相关文章:

  • 2022-03-08
  • 2018-12-26
  • 2022-01-14
  • 2021-07-20
  • 2021-10-20
  • 2022-12-23
  • 2022-02-03
猜你喜欢
  • 2021-07-17
  • 2021-10-16
  • 2021-09-30
  • 2022-03-06
  • 2022-01-29
  • 2021-11-14
  • 2022-02-27
相关资源
相似解决方案