twisted internet.reactor部分 源码分析
twisted.internet.reactor 是进行所有twisted事件循环的地方。
reactor在1个python进程中只能有一个。
在windows下用的是select。linux下epool。mac下是pool,这点和socketserver,tornado的都一样哈()。
源码位于twisted.internet.default._getInstallFunction
try:
if platform.isLinux():
try:
from twisted.internet.epollreactor import install
except ImportError:
from twisted.internet.pollreactor import install
elif platform.getType() == 'posix' and not platform.isMacOSX():
from twisted.internet.pollreactor import install
else:
from twisted.internet.selectreactor import install
except ImportError:
from twisted.internet.selectreactor import install
return install
看SelectReactor
try:
from twisted.internet.win32eventreactor import _ThreadedWin32EventsMixin
except ImportError:
_extraBase = object
else:
_extraBase = _ThreadedWin32EventsMixin
@implementer(IReactorFDSet)
class SelectReactor(posixbase.PosixReactorBase, _extraBase):
"""
A select() based reactor - runs on all POSIX platforms and on Win32. @ivar _reads: A set containing L{FileDescriptor} instances which will be
checked for read events. @ivar _writes: A set containing L{FileDescriptor} instances which will be
checked for writability.
"""
balabala........
主要功能实现在posixbase.PosixReactorBase这个类。然后根据os的不同,混入不同的多线程mixin。
每种Reactor要实现IReactorFDSet的接口。接口定义的是管理 端口状态的方法。比如addReader,addWriter。。还是放代码吧。
class IReactorFDSet(Interface):
"""
Implement me to be able to use L{IFileDescriptor} type resources. This assumes that your main-loop uses UNIX-style numeric file descriptors
(or at least similarly opaque IDs returned from a .fileno() method)
""" def addReader(reader):
"""
I add reader to the set of file descriptors to get read events for. @param reader: An L{IReadDescriptor} provider that will be checked for
read events until it is removed from the reactor with
L{removeReader}. @return: C{None}.
""" def addWriter(writer):
"""
I add writer to the set of file descriptors to get write events for. @param writer: An L{IWriteDescriptor} provider that will be checked for
write events until it is removed from the reactor with
L{removeWriter}. @return: C{None}.
""" def removeReader(reader):
"""
Removes an object previously added with L{addReader}. @return: C{None}.
""" def removeWriter(writer):
"""
Removes an object previously added with L{addWriter}. @return: C{None}.
""" def removeAll():
"""
Remove all readers and writers. Should not remove reactor internal reactor connections (like a waker). @return: A list of L{IReadDescriptor} and L{IWriteDescriptor} providers
which were removed.
""" def getReaders():
"""
Return the list of file descriptors currently monitored for input
events by the reactor. @return: the list of file descriptors monitored for input events.
@rtype: C{list} of C{IReadDescriptor}
""" def getWriters():
"""
Return the list file descriptors currently monitored for output events
by the reactor. @return: the list of file descriptors monitored for output events.
@rtype: C{list} of C{IWriteDescriptor}
"""
没什么奇怪的地方。。
所以就不看这边了。这地方太熟了。除去了多线程的Mixin和端口reader,writer的接口,剩下的就是reactor主要部分啦
接下来看下posixbase.PosixReactorBase这个基类。为什么是PosixReactorBase而不是ReactorBase呢,因为后者是他的父类。。
@implementer(IReactorTCP, IReactorUDP, IReactorMulticast)
class PosixReactorBase(_SignalReactorMixin, _DisconnectSelectableMixin,
ReactorBase):
""" A basis for reactors that use file descriptors. @ivar _childWaker: C{None} or a reference to the L{_SIGCHLDWaker}
which is used to properly notice child process termination.
""" # Callable that creates a waker, overrideable so that subclasses can
# substitute their own implementation:
IReactorTCP, IReactorUDP, IReactorMulticast是定义了PosixReactorBase的套接字方面的一些接口。接口定义的方法不多,源码中实现的还有ListenSSL,listenUNIX,listenUNIXDatagram,甚至adoptStreamPort去兼容IPV6。。he,真不少。 再继续分析PosixReactorBase的混入。
1. _SignalReactorMixin,前面说的单进程只有一个Reactor就是在这里实现的啦。。
而且reactor也不能在stop后再run起来。Reactor在一个进程中只能run一次。。
2、_DisconnectSelectableMixin。这个MixIn只实现了一个方法。来处理haf_close,原理还是在于TCP的4次挥手再见。根据是否完成挥手操作,区分ConnectionLost还是ConnectionDone.
然后终于看这个ReactorBase类了,抽丝剥茧,最后看到的就是就是这个东西。。
@implementer(IReactorCore, IReactorTime, IReactorPluggableResolver)
class ReactorBase(object):
"""
Default base class for Reactors. @type _stopped: C{bool}
@ivar _stopped: A flag which is true between paired calls to C{reactor.run}
and C{reactor.stop}. This should be replaced with an explicit state
machine. @type _justStopped: C{bool}
@ivar _justStopped: A flag which is true between the time C{reactor.stop}
is called and the time the shutdown system event is fired. This is
used to determine whether that event should be fired after each
iteration through the mainloop. This should be replaced with an
explicit state machine. @type _started: C{bool}
@ivar _started: A flag which is true from the time C{reactor.run} is called
until the time C{reactor.run} returns. This is used to prevent calls
to C{reactor.run} on a running reactor. This should be replaced with
an explicit state machine. @ivar running: See L{IReactorCore.running} @ivar _registerAsIOThread: A flag controlling whether the reactor will
register the thread it is running in as the I/O thread when it starts.
If C{True}, registration will be done, otherwise it will not be.
"""
在这里,实现的是IReactorCore,IReactorTime, IReactorPluggableResolver三个接口。。(核心两个字写在脸上了。“打脸”就朝这里)
IReactor实现了什么呢。就是Reacor的主要特点。Event操作。Reactor的开关,和 DNS查询。。。BTW,这点比BaseHTTPServer想的好多了。
后者就直接去查DNS了。而这里放入了事件循环当中,没有去阻塞进程。。
IReactorTime是有关defer对象处理,seconds和callLater什么的。
IReactorPluggableResolver是提供了关于查询DNS的接口,方便自己去实现缓存或者预设DNS,本地DNS 什么的。。
#歇一会儿。。
twisted internet.reactor部分 源码分析的更多相关文章
- twisted 源码分析一:reactor 单例
一个twisted进程只会有一个reactor反应器,下面我们来看看twisted是怎样实现这个单例反应器的, 路径:twisted\internet\reactor.py 主要代码如下: impor ...
- Twisted源码分析系列01-reactor
转载自:http://www.jianshu.com/p/26ae331b09b0 简介 Twisted是用Python实现的事件驱动的网络框架. 如果想看教程的话,我觉得写得最好的就是Twisted ...
- twisted reactor 实现源码解析
twisted reactor 实现源码解析 1. reactor源码解析 1.1. 案例分析代码: from twisted.internet import protocol fro ...
- netty源码分析之揭开reactor线程的面纱(二)
如果你对netty的reactor线程不了解,建议先看下上一篇文章netty源码分析之揭开reactor线程的面纱(一),这里再把reactor中的三个步骤的图贴一下 reactor线程 我们已经了解 ...
- Netty源码分析之Reactor线程模型详解
上一篇文章,分析了Netty服务端启动的初始化过程,今天我们来分析一下Netty中的Reactor线程模型 在分析源码之前,我们先分析,哪些地方用到了EventLoop? NioServerSocke ...
- Python twisted事件驱动网络框架 源码剖析
一.Twisted简介 Twisted是一个事件驱动的网络框架,其中包含了诸多功能,例如:网络协议.线程.数据库管理.网络操作.电子邮件等. 事件驱动简而言之,事件驱动分为二个部分:第一,注册事件:第 ...
- MyCat源码分析系列之——前后端验证
更多MyCat源码分析,请戳MyCat源码分析系列 MyCat前端验证 MyCat的前端验证指的是应用连接MyCat时进行的用户验证过程,如使用MySQL客户端时,$ mysql -uroot -pr ...
- jQuery实现DOM加载方法源码分析
传统的判断dom加载的方法 使用 dom0级 onload事件来进行触发所有浏览器都支持在最初是很流行的写法 我们都熟悉这种写法: window.onload=function(){ ... } 但 ...
- Redis学习——ae事件处理源码分析
0. 前言 Redis在封装事件的处理采用了Reactor模式,添加了定时事件的处理.Redis处理事件是单进程单线程的,而经典Reator模式对事件是串行处理的.即如果有一个事件阻塞过久的话会导致整 ...
随机推荐
- C#_delegate和事件 - 如果金额小于0则触发事件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 分享一个字数限制和统计的UITextView分类方法
- (NSUInteger)letterCountWithLimits:(NSInteger)limits { NSString *toBeString = self.text; NSUInteger ...
- Linux批量替换文件内容
问题描述:现在需要将rack1目录下*.send文件中的"-ip="替换成“-localIp=10.0.0.1/n-ip=” 刚才那个批量文本内容替换,只能替换内存中的内容,并不会 ...
- Arp欺骗攻击的另类应用之屌丝泡妞记
http://www.2cto.com/Article/201210/163974.html arp欺骗,我想大家都应该知道怎么回事了.不知道的去问度娘... 就不废话了,还是直接上图上教程比 ...
- logstash-input-file 参数说明
参数 close_older close_older: # This has different implications depending on if a file is being tailed ...
- 关于SWT常用组件(按钮,复选框,单选框(Button类))
Button是SWT中最常用的组件.Button类的继承关系图: Button类的构造方法是newe Button(Composite parent,int style)它有两个参数: 第一个参数:是 ...
- 关于Integer类中parseInt()和valueOf()方法的区别以及int和String类性的转换.以及String类valueOf()方法
Integer类中的. 关于parseInt()方法的API文档. 返回的是int类型的 关于valueOf()方法的API文档 返回的是Integer类型的. 关于intValue()方法的API ...
- [MSDN] 使用 SharePoint 2013 中的 JavaScript 库代码完成基本操作
MSDN:http://msdn.microsoft.com/zh-cn/library/jj163201.aspx 了解如何编写代码以在 SharePoint 2013 中使用 JavaScript ...
- 揭开CSS3媒体查询迷雾(min-width和max-width)
本文参考MichelleKlann的Media Queries Demystified: Min-Width and Max-Width 媒体查询(media queries)是响应式设计(Respo ...
- 【C#4.0图解教程】笔记(第1章~第8章)
第1章 C#和.NET框架 1..NET框架的组成 .NET框架由三部分组成(严格来说只有CLR和FCL(框架类库)两部分),如图 执行环境称为:CLR(公共语言运行库),它在运行期管理程序的执行. ...