IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> Twisted框架中NIO架构的个人理解 -> 正文阅读

[Python知识库]Twisted框架中NIO架构的个人理解

? ? ? ? 最近假期在家跟着《Python核心编程》学习这门语言,在网络编程这一章中书上提到了Twisted框架,但介绍寥寥。于是打算跟着函数过一遍流程,但完整流程对一个初学者来说过于复杂,于是退而求其次尝试理解其中的NIO架构,以下内容为个人理解,如有错误,还烦请不吝赐教。

? ? ? ? 从书上给的服务端代码开始:

from twisted.internet import protocol,reactor

PORT=17025

class tspro(protocol.Protocol):
    def connectionMade(self):
        clnt=self.clnt=self.transport.getPeer()
        print('connected from:',clnt)
    def dataReceived(self, data):
        self.transport.write(data)
    
factory=protocol.Factory()
factory.protocol=tspro
print('waiting for connection')
reactor.listenTCP(PORT,factory)
reactor.run()

? ? ? ? 可以看到从twisted中引入了两个模块:factory与reactor。factory作为创建实例的工厂先不深究,重点在于最后的reactor.listen()与reactor.run()。直接点进reactor发现代码如下:

import sys

del sys.modules["twisted.internet.reactor"]
from twisted.internet import default

default.install()

? ? ? ? ?里面调用了default.install()函数,继续查看:

   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

? ? ? ? 跨平台适配,查看针对Linux系统创建epollreactor类的代码

def install():
    """
    Install the epoll() reactor.
    """
    p = EPollReactor()
    from twisted.internet.main import installReactor

    installReactor(p)
    def __init__(self):
        """
        Initialize epoll object, file descriptor tracking dictionaries, and the
        base class.
        """
        # Create the poller we're going to use.  The 1024 here is just a hint
        # to the kernel, it is not a hard maximum.  After Linux 2.6.8, the size
        # argument is completely ignored.
        self._poller = epoll(1024)
        self._reads = set()
        self._writes = set()
        self._selectables = {}
        self._continuousPolling = posixbase._ContinuousPolling(self)
        posixbase.PosixReactorBase.__init__(self)

    def _add(self, xer, primary, other, selectables, event, antievent):
        """
        Private method for adding a descriptor from the event loop.

        It takes care of adding it if  new or modifying it if already added
        for another state (read -> read/write for example).
        """
        fd = xer.fileno()
        if fd not in primary:
            flags = event
            # epoll_ctl can raise all kinds of IOErrors, and every one
            # indicates a bug either in the reactor or application-code.
            # Let them all through so someone sees a traceback and fixes
            # something.  We'll do the same thing for every other call to
            # this method in this file.
            if fd in other:
                flags |= antievent
                self._poller.modify(fd, flags)
            else:
                self._poller.register(fd, flags)

            # Update our own tracking state *only* after the epoll call has
            # succeeded.  Otherwise we may get out of sync.
            primary.add(fd)
            selectables[fd] = xer

? ? ? ? 在这里驱动本次reactor的事件的句柄被提取放入self._reads或self._writes组成事件表,配合reactor.run()循环中调用seletreactor建立readylist,通过遍历readylist针对不同类型的读、写或连接建立事件调用自己定义的protocol.connectionMade()或protocol.dataReceive()等方法进行并发处理。

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-07-13 17:25:22  更:2021-07-13 17:26:59 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/4 17:28:35-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码