Source -import reactor analytical notes -twisted

Source -import reactor analytical notes -twisted

 

1. twisted source parsing -1

twisted reactor realization of the principle:

first step:

from twisted.internet import reactor

pr_type(reactor)

result:

 

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

 

1.1. Twisted.internet.reactor.py source

github find the corresponding source code:

from __future__ import division, absolute_import

 

import sys

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

from twisted.internet import default

default.install()

1.1.1. Issues

Curiously del statement, and delete references will complain at the time of testing

Test code is as follows:

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

del sys.modules['test_unit.m1']

Error:

KeyError: 'test_unit.m1'

 

It is found because retest from del to perform a process import m1

The following tests are normal:

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

 

Run temp.py results:

<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'>

 

Interpretation of the results:

M1 = import operation will be performed after completion of execution m1.py () of

Then use about locals () variable m1 see: 'm1': 6

 

1.2. Twisted.internet.selectreactor.py source

carry on

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 source

carry on:

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

In simple terms, here, we have done two things

  1. The system variable reactor = SelectReactor ()
  2. sys.modules['twisted.internet.reactor'] 设为SelectReactor()

Verify the following:

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

result:

<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 has been found, the following look at what it does after running:

res = reactor.run
pr_type(res)

result:

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

Look at github get: twisted / internet / base.py

class _SignalReactorMixin(object):

    def startRunning(self, installSignalHandlers=True):

        """

        PosixReactorBase _SignalReactorMixin parent class and has the function ReactorBase, but _SignalReactorMixin front mounting mro order, then, will the first call _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.')

 

Here is the main loop.

 

Guess you like

Origin www.cnblogs.com/wodeboke-y/p/11004515.html