Chinaunix首页 | 论坛 | 博客

ken

  • 博客访问: 18857
  • 博文数量: 12
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 150
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-23 10:11
文章分类
文章存档

2011年(1)

2008年(11)

我的朋友
最近访客

分类: LINUX

2008-09-23 14:20:22

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了。


阅读(479) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~