免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1018 | 回复: 0
打印 上一主题 下一主题

twisted学习笔记之一:综述和reactor概述 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-23 14:20 |只看该作者 |倒序浏览
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 sysprint sys.modules['twisted.internet.reactor']
有了这两句,你就可以放心大胆的告诉别人你用的就是epoll了。
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/80381/showart_1218244.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP