调用reactor.run(),就会调用到mainloop函数,从而调用到select或epoll,监控fd的读写。

posixbase.py:

    def listenTCP(self, port, factory, backlog=50, interface=''):
p = tcp.Port(port, factory, backlog, interface, self)
p.startListening()#会调用self.startReading(),再调用self.reactor.addReader(self)
#把自己加入epoll
return p def connectTCP(self, host, port, factory, timeout=30, bindAddress=None):
c = tcp.Connector(host, port, factory, timeout, bindAddress, self)
c.connect()
return c

factory类负责connect的管理,比如connect的建立、丢失、失败等,procotol是负责数据的接收。

 tcp.Connector(host, port, factory, timeout, bindAddress, self)

Connector类有factory,由factory可以找出procotol协议,协议主要是指tcp、process、ssl等。
class ClientCreator:
"""
Client connections that do not require a factory. The various connect* methods create a protocol instance using the given
protocol class and arguments, and connect it, returning a Deferred of the
resulting protocol instance. Useful for cases when we don't really need a factory. Mainly this
is when there is no shared state between protocol instances, and no need
to reconnect. The C{connectTCP}, C{connectUNIX}, and C{connectSSL} methods each return a
L{Deferred} which will fire with an instance of the protocol class passed to
L{ClientCreator.__init__}. These Deferred can be cancelled to abort the
connection attempt (in a very unlikely case, cancelling the Deferred may not
prevent the protocol from being instantiated and connected to a transport;
if this happens, it will be disconnected immediately afterwards and the
Deferred will still errback with L{CancelledError}).
""" def __init__(self, reactor, protocolClass, *args, **kwargs):
self.reactor = reactor
self.protocolClass = protocolClass
self.args = args
self.kwargs = kwargs def _connect(self, method, *args, **kwargs):
"""
Initiate a connection attempt. @param method: A callable which will actually start the connection
attempt. For example, C{reactor.connectTCP}. @param *args: Positional arguments to pass to C{method}, excluding the
factory. @param **kwargs: Keyword arguments to pass to C{method}. @return: A L{Deferred} which fires with an instance of the protocol
class passed to this L{ClientCreator}'s initializer or fails if the
connection cannot be set up for some reason.
"""
def cancelConnect(deferred):
connector.disconnect()
if f.pending is not None:
f.pending.cancel()
d = defer.Deferred(cancelConnect)#会生成一个延迟对象
f = _InstanceFactory(
self.reactor, self.protocolClass(*self.args, **self.kwargs), d)
connector = method(factory=f, *args, **kwargs)
return d def connectTCP(self, host, port, timeout=30, bindAddress=None):
"""
Connect to a TCP server. The parameters are all the same as to L{IReactorTCP.connectTCP} except
that the factory parameter is omitted. @return: A L{Deferred} which fires with an instance of the protocol
class passed to this L{ClientCreator}'s initializer or fails if the
connection cannot be set up for some reason.
"""
return self._connect(
self.reactor.connectTCP, host, port, timeout=timeout,
bindAddress=bindAddress)#返回一个延迟对象
class BaseConnector:
"""Basic implementation of connector. State can be: "connecting", "connected", "disconnected"
"""
timeoutID = None
factoryStarted = 0 def __init__(self, factory, timeout, reactor):
self.state = "disconnected"
self.reactor = reactor
self.factory = factory
self.timeout = timeout def disconnect(self):
"""Disconnect whatever our state is."""
if self.state == 'connecting':
self.stopConnecting()
elif self.state == 'connected':
self.transport.loseConnection() def connect(self):
"""Start connection to remote server."""
if self.state != "disconnected":
raise RuntimeError("can't connect in this state") self.state = "connecting"
if not self.factoryStarted:
self.factory.doStart()
self.factoryStarted = 1
self.transport = transport = self._makeTransport()#创建一个client端
if self.timeout is not None:
self.timeoutID = self.reactor.callLater(self.timeout, transport.failIfNotConnected, error.TimeoutError())
self.factory.startedConnecting(self)

重要结论:调用

twisted reactor分析的更多相关文章

  1. twisted reactor 实现源码解析

    twisted reactor 实现源码解析 1.      reactor源码解析 1.1.    案例分析代码: from twisted.internet import protocol fro ...

  2. twisted reactor calllater实现

    twisted reactor calllater实现 1.      calllater实现代码 测试源码: from twisted.internet import reactor from tw ...

  3. (三)认识twisted reactor

    一.reactor是单线程模型,简单粗暴,也就是说网络IO和我们的业务逻辑一般是在一个线程里,其中网络IO通过event loop的方式去异步执行,效率也很高.看下官网的这幅图,比较清晰 twiste ...

  4. twisted reactor执行流程

    #reactorbase的主循环 def mainLoop(self): while self._started: try: while self._started: # Advance simula ...

  5. 理解twisted中的reactor和deferred(一)

    Deferred是一个延迟加载对象,这个概念类似于tornado future,是调用异步操作返回的一个对象,其中包括了操作成功后的回调处理,错误后的回调处理. 简单讲,当我们需要执行一个耗时操作,比 ...

  6. 笔记-twisted源码-import reactor解析

    笔记-twisted源码-import reactor解析 1.      twisted源码解析-1 twisted reactor实现原理: 第一步: from twisted.internet ...

  7. scrapy爬虫具体案例步骤详细分析

    scrapy爬虫具体案例详细分析 scrapy,它是一个整合了的爬虫框架, 有着非常健全的管理系统. 而且它也是分布式爬虫, 它的管理体系非常复杂. 但是特别高效.用途广泛,主要用于数据挖掘.检测以及 ...

  8. scrapy爬行乌云网公开漏洞程序的分析

    # -*- coding: utf-8 -*- from datetime import datetime import pymongo import scrapy from wooyun.items ...

  9. python网络编程——SocketServer/Twisted/paramiko模块

    在之前博客C/S架构的网络编程中,IO多路复用是将多个IO操作复用到1个服务端进程中进行处理,即无论有多少个客户端进行连接请求,服务端始终只有1个进程对客户端进行响应,这样的好处是节省了系统开销(se ...

随机推荐

  1. 【java】之equals和==区别

      Java中数据类型可以分为两类 1.基本数据类型(byte,short,char,int,float,double,long,boolean) 2.复合数据类型(类,String等) Δ在基本数据 ...

  2. bzoj4398: 福慧双修

    正边权无向图,一条边两个方向权值不一定相同,求经过点1的最小简单环 简单环包含了点1的一条出边和一条入边,且这两条边不同,因此可以枚举这两条边的编号的二进制表示中哪一位不同,用最短路求此时的最优解,时 ...

  3. Python实现简单的网页抓取

    现在开源的网页抓取程序有很多,各种语言应有尽有. 这里分享一下Python从零开始的网页抓取过程 第一步:安装Python 点击下载适合的版本https://www.python.org/ 我这里选择 ...

  4. 峰Redis学习(4)Redis 数据结构(List的操作)

    第四节:Redis 数据结构之List 类型 存储list: ArrayList使用数组方式 LinkedList使用双向链接方式   双向链接表中增加数据 双向链接表中删除数据   存储list常用 ...

  5. openVPN设置本地密码验证

    wget https://git.io/vpn -O openvpn-install.sh && bash openvpn-install.sh https://github.com/ ...

  6. Jmeter(十九)Logic Controllers 之 Module Controller and Include Controller

    Module Controller ---模块控制器 测试计划设置“独立运行没每个线程组” 线程组2中使用Module Controller执行线程组1中的Sampler: 紧接着,将线程组1disa ...

  7. Sublime下MarkDown插件实现编辑和实时预览并转换成HTML格式

    最近在使用markdown做笔记,编辑器Sublime Text3用起来很轻巧,现在让他支持markdown的语法并且可以实时预览. 安装准备——安装Package Control Package C ...

  8. 几种常见NPE

    NPE(Null Point Exception的简称) 1.Map下的NPE 直接上代码: public class User { private Integer id; private Strin ...

  9. confluence部署与破解

    一.confluence安装 #安装环境环境 centos7.jdk8.mysql5.7.Confluence6.14.1 confluence下载地址 wget https://product-do ...

  10. xgboost的sklearn接口和原生接口参数详细说明及调参指点

    from xgboost import XGBClassifier XGBClassifier(max_depth=3,learning_rate=0.1,n_estimators=100,silen ...