笔记-twisted源码-import reactor解析

1.      twisted源码解析-1

twisted reactor实现原理:

第一步:

from twisted.internet import reactor

pr_type(reactor)

结果:

<twisted.internet.selectreactor.SelectReactor object at 0x0000001D5D82B748> <class 'twisted.internet.selectreactor.SelectReactor'>

1.1.    twisted.internet.reactor.py源码

github中找到相应源码:

from __future__ import division, absolute_import

import sys

del sys.modules['twisted.internet.reactor']

from twisted.internet import default

default.install()

1.1.1.   问题

奇怪的是del语句,在测试时引用又删除会报错

测试代码如下:

import sys
print(sys.modules['test_unit.m1'])

del sys.modules['test_unit.m1']

报错:

KeyError: 'test_unit.m1'

复测发现是因为从del 到import m1有一个执行过程

下面的测试正常:

temp.py

from test_unit import m1

pr_type(m1)
sys.modules['test_unit.m1'] = 5
pr_type(sys.modules['test_unit.m1'])
pr_type(m1)

test_unit.m1.py

import sys
print(sys.modules['test_unit.m1'])

del sys.modules['test_unit.m1']
sys.modules['test_unit.m1'] = 6

运行temp.py结果:

<module 'test_unit.m1' from
'E:\\python\\爬虫\\scrapy\\fangchan_pro\\fangchan_code\\test_unit\\m1.py'>

6
<class 'int'>

5
<class 'int'>

6
<class 'int'>

结果释义:

在m1.py执行完成后才会执行m1 = import()的操作

这时使用locals()可以看到m1变量:'m1': 6

1.2.   
twisted.internet.selectreactor.py源码

继续

def install():

"""Configure the twisted mainloop to be run using the
select() reactor.

"""

reactor = SelectReactor()

from twisted.internet.main import installReactor

installReactor(reactor)

1.3.   
twisted.internet.main源码

继续:

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

import sys

if 'twisted.internet.reactor' in sys.modules:

raise error.ReactorAlreadyInstalledError("reactor already
installed")

twisted.internet.reactor = reactor

sys.modules['twisted.internet.reactor']
= reactor

简单来说,到这里,做了两件事

  1. 将系统变量reactor = SelectReactor()
  2. sys.modules['twisted.internet.reactor'] 设为SelectReactor()

验证如下:

import sys
pr_type(reactor)
pr_type(sys.modules['twisted.internet.reactor'])

结果:

<twisted.internet.selectreactor.SelectReactor
object at 0x000000BD4943B400> <class 'twisted.internet.selectreactor.SelectReactor'>

<twisted.internet.selectreactor.SelectReactor
object at 0x000000BD4943B400> <class
'twisted.internet.selectreactor.SelectReactor'>

1.4.   
reactor.run()

reactor已经找到了,下面看一下它在运行后做什么:

res = reactor.run
pr_type(res)

结果:

<bound method _SignalReactorMixin.run of
<twisted.internet.selectreactor.SelectReactor object at
0x000000C61740B6D8>> <class 'method'>

在github中查找得到:twisted/internet/base.py

class _SignalReactorMixin(object):

def
startRunning(self, installSignalHandlers=True):

"""

PosixReactorBase的父类_SignalReactorMixin和ReactorBase都有该函数,但是_SignalReactorMixin在前,安装mro顺序的话,会先调用_SignalReactorMixin中的。

"""

self._installSignalHandlers = installSignalHandlers

ReactorBase.startRunning(self)

def run(self,
installSignalHandlers=True):

self.startRunning(installSignalHandlers=installSignalHandlers)

self.mainLoop()

def mainLoop(self):

while
self._started:

try:

while
self._started:

# Advance simulation time in delayed event

#
processors.

self.runUntilCurrent()

t2 =
self.timeout()

t =
self.running and t2

self.doIteration(t)

except:

log.msg("Unexpected error in main loop.")

log.err()

else:

log.msg('Main loop terminated.')

这里就是主循环了。

05-18 18:55