#twisted的安装反应堆函数
def installReactor(reactor): """ Install reactor C{reactor}. @param reactor: An object that provides one or more IReactor* interfaces. """ # this stuff should be common to all reactors. import twisted.internet#导入包,会在sys.modules添加一个module import sys if 'twisted.internet.reactor' in sys.modules: raise error.ReactorAlreadyInstalledError("reactor already installed") twisted.internet.reactor = reactor#为twisted.internet模块添加一个属性 sys.modules['twisted.internet.reactor'] = reactor#手动添加一个模块
>>> import twisted.internet >>> print type(twisted.internet) <type 'module'> >>> twisted.internet.__name__ 'twisted.internet' >>> twisted.internet.reactor = 'ok' >>> twisted.internet.reactor 'ok'
reactor的选择,不同平台会选择不同的reactor,比如linux会选epollreactor,从internet导入reactor.py时,执行下面代码
from __future__ import division, absolute_import import sys del sys.modules['twisted.internet.reactor'] from twisted.internet import default default.install()
#default.py模块返回安装函数
def _getInstallFunction(platform): 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 install = _getInstallFunction(platform)
调用default.install()时就安装了reactor