twisted programming - noonecare/python GitHub Wiki
twisted 是事件驱动,异步IO 写的框架。twisted 常常被用来写网络服务,写网络服务时, twisted 比 tornado 的扩展性要好: 不仅可以实现 http 协议,twisted 可以实现很多很多的协议,比如 SMTP 等等。
事件驱动(reactor 和 Deferred 实现了异步和同步)
- 所有事件驱动的 framework 一定有个对象表示 周期 poll 事件的 Loop, 在 torando 中是 IOLoop, 在 twisted 中是 reactor, 正是 reactor 实现了异步 。(reactor 和 IOLoop 有个明显的不同: 在 reactor , event-handler 在一次 IOLoop iteration 中不可能耗用太长时间,因为 reactor 添加了时间事件,耗时到一定时间,自动进入下一个 IOLoop iteration; tornado IOLoop 没有自动添加时间事件,event-handler 可能在一次 IOLoop Iteration 中耗用很长时间)
- Deferred 是 twisted 中实现同步的概念, 注册到 Deferred 的 callback 一定是顺序执行的。
网络服务
twisted 涉及到了 TCP/IP 中很多的协议,组件。
- Serve Side
- Factory
- Protocol
- Site
- Resource
- Client Side
- Agent(这就是个浏览器)
- transport
最后,我对 twisted 的了解有限,所有了解来自于 twisted-network-programming-essentials