twisted reactor分析
调用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分析的更多相关文章
- twisted reactor 实现源码解析
twisted reactor 实现源码解析 1. reactor源码解析 1.1. 案例分析代码: from twisted.internet import protocol fro ...
- twisted reactor calllater实现
twisted reactor calllater实现 1. calllater实现代码 测试源码: from twisted.internet import reactor from tw ...
- (三)认识twisted reactor
一.reactor是单线程模型,简单粗暴,也就是说网络IO和我们的业务逻辑一般是在一个线程里,其中网络IO通过event loop的方式去异步执行,效率也很高.看下官网的这幅图,比较清晰 twiste ...
- twisted reactor执行流程
#reactorbase的主循环 def mainLoop(self): while self._started: try: while self._started: # Advance simula ...
- 理解twisted中的reactor和deferred(一)
Deferred是一个延迟加载对象,这个概念类似于tornado future,是调用异步操作返回的一个对象,其中包括了操作成功后的回调处理,错误后的回调处理. 简单讲,当我们需要执行一个耗时操作,比 ...
- 笔记-twisted源码-import reactor解析
笔记-twisted源码-import reactor解析 1. twisted源码解析-1 twisted reactor实现原理: 第一步: from twisted.internet ...
- scrapy爬虫具体案例步骤详细分析
scrapy爬虫具体案例详细分析 scrapy,它是一个整合了的爬虫框架, 有着非常健全的管理系统. 而且它也是分布式爬虫, 它的管理体系非常复杂. 但是特别高效.用途广泛,主要用于数据挖掘.检测以及 ...
- scrapy爬行乌云网公开漏洞程序的分析
# -*- coding: utf-8 -*- from datetime import datetime import pymongo import scrapy from wooyun.items ...
- python网络编程——SocketServer/Twisted/paramiko模块
在之前博客C/S架构的网络编程中,IO多路复用是将多个IO操作复用到1个服务端进程中进行处理,即无论有多少个客户端进行连接请求,服务端始终只有1个进程对客户端进行响应,这样的好处是节省了系统开销(se ...
随机推荐
- response.sendRedirect传递参数和转向
response.sendRedirect是通过浏览器来做转向的. 假设在A.jsp页面设置request.setAttribute("username","admin& ...
- SpringMVC拦截器配置
1.首先在springmvc.xml中添加配置 <mvc:interceptors> <mvc:interceptor> <mvc:mapping path=" ...
- locust -基础框架
# coding=utf-8from locust import HttpLocust, TaskSet, taskimport requests # 定义用户行为class UserBehavior ...
- 1124 Raffle for Weibo Followers (20 分)
1124 Raffle for Weibo Followers (20 分) John got a full mark on PAT. He was so happy that he decided ...
- go语言学习--指针数组和数组指针
数组指针(也称行指针)定义 int (*p)[n];()优先级高,首先说明p是一个指针,指向一个整型的一维数组,这个一维数组的长度是n,也可以说是p的步长.也就是说执行p+1时,p要跨过n个整型数据的 ...
- win10以上系统设定PPTP自动拨号
:bohaorasdial adsl 123 123if not %errorlevel% == 0 goto :bohaoexit rasdial adsl 123 123 rasdial是开始拨号 ...
- Quidway S系列交换机
一. 华为交换机设备,以Quidway S系列命名,广泛适用于企业网.园区网和运营网络.Quidway S系列交换机包括: 接入层交换机 汇聚层交换机 核心层交换机 1.接入层交换机 包含两个系列: ...
- [UE4]透明按钮
Background Color的透明度设置为0,就能让按钮背景完全透明,但是按钮里面的子控件并不会跟着透明
- python中变量的缓存机制
同一文件中, 变量的缓存机制 (在此范围内的相同值内存地址一样) Number: int: -5 ~ 正无穷 float: 非负数 bool: ...
- java 根据日期获取星期
private String getWeek(String date) { String[] arr=date.split("-"); Calendar calendar = Ca ...