现在的位置: 首页 > 综合 > 正文

twisted学习笔记之:综述和reactor概述

2018年03月22日 ⁄ 综合 ⁄ 共 3231字 ⁄ 字号 评论关闭

我是今年暑假开始正式加入到python学习正营的。一开始学习,我就深深的喜欢上了它。因为以前一直是用c和c++,虽然也学过java但课程结束后就
没再用过了。所以用的最多的还是c和c++,这两种语言我都是很喜欢的,感觉各自都有自己的优点。当看了《thinking in
c++》之后,我才真正知道了c++的强大和很多奥妙,c++是一门智者使用的编程语言。任何一个想真正领略c++灵魂的人估计都得十年八年的修为,否则
说自己精通c++估计也就是在侮辱自己吧。

   
python是一门比较新的编程语言,面向对象的脚本语言。很多人一听到脚本语言就联想到shell和JavaScript了,其实python跟这些语
言相比起来,有一些脚本语言的共性,但更多的是python的新特性。它的强大不是我三言两语可以说得清楚的。学python最好的当然是看bt源码
了,bt公布的最新的源码是5.2的,在bittorrent源码上可以下载,而BT协议规范也在BT specifications上有英文原文。

    好了,话归正题。说了这么多,跟题目的twisted好像没有一点关系呢?http://twistedmatrix.com/trac/这个网站告诉我们Twisted is an event-driven networking engine written in Python and licensed under the MIT license.twisted
是python里面公认的很牛的网络编程框架。学python网络编程的如果不学twisted,估计也就只能算是了解python网络编程吧,就如同开
发网站要用django是一样的,二者都是python下有名的框架。twisted是基于单线程的事件驱动的网络引擎。关于它的学习资料比较少,而且中
文的就更少了,所以学习twisted一定要硬着头皮看英文文档,也就是它的twisted documentation,在这里基本可以找到你所需要的所有基础知识。尤其是core documentation 和example里面都讲了很多示例,这些示例如果都通通的运行一遍,那么你的twisted已经可以算入门了。

   我主要是用twisted的工厂和协议框架编写了一个内部的内容分发网络的Tracker服务器,不是基于标准bt协议的,如果要学习,最好还是按照标准BT协议。前面也给了网址。至于如何使用twisted,我会在后续文章详细介绍。
  
   本文先介绍twisted的两种工作方式,reactor 和 application方式。
The reactor is the core of the event loop within Twisted -- the loop
which drives applications using Twisted. The reactor provides basic
interfaces to a number of services, including network communications,
threading, and event dispatching.
reactor是twisted事件循环的核心,它提供了一些服务的基本接口,像网络通信、线程和事件的分发。

详细的关于reactor的介绍见twisted core documentation里面的Low-Level Twisted一章的第一节Reactor Overview.里面详细介绍了各种reactor的安装和使用。
我所知道的reactor有以下几个

 reactor platform  Usage
 IOCPReactor  win32 from twisted.internet import iocpreactor     iocpreactor.reactor.install()

from twisted.internet import reactor

 selectReactor  win32, posix  from twisted.internet import reactor
 pollReactor  posix  from twisted.internet import pollreactor
 pollreactor.install()

from twisted.internet import reactor

 epollReactor  linux2.6  from twisted.internet import epollreactor
 epollreactor.install()

from twisted.internet import reactor

 kqueueReactor  BSD系列  from twisted.internet import kqreactor
 kqreactor.install()

from twisted.internet import reactor

以上几种就是使用最多的几种reactor了,除了kqueueReactor我没有使用过以外,其他的都使用过了。都能正常工作。建议编程序的时候实现根据不同的平台选择最佳的reactor。
系统默认使用的是selectreactor。

下面给出一个小例子:

from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor

### Protocol Implementation

# This is just about the simplest possible protocol
class Echo(Protocol):
    def dataReceived(self, data):
        """As soon as any data is received, write it back."""
        self.transport.write(data)

def main():
    f = Factory()
    f.protocol = Echo
    reactor.listenTCP(8000, f)
    reactor.run()

if __name__ == '__main__':
    main()


这个是调用默认的selectreactor.

下面我把它改为epollreactor

from twisted.internet.protocol import Protocol, Factory

### Protocol Implementation

# This is just about the simplest possible protocol
class Echo(Protocol):
    def dataReceived(self, data):
        """As soon as any data is received, write it back."""
        self.transport.write(data)

def main():
    f = Factory()
    f.protocol = Echo
   
    from twisted.internet import epollreactor
    epollreactor.install()
   
    from twisted.internet import reactor

    reactor.listenTCP(8000, f)
    reactor.run()

if __name__ == '__main__':
    main()

这样程序使用的就是epollreactor了,有人说,我怎么知道它到底使用的是什么reactor呢?
只需要在你的程序中添加下面两行就可以知道了:
import sys
print sys.modules['twisted.internet.reactor']
有了这两句,你就可以放心大胆的告诉别人你用的就是epoll了。

reactor暂时讲到这里,我会随时更新的。
至于application,我打算在后面专门用一章来介绍。

抱歉!评论已关闭.