`
huangro
  • 浏览: 333394 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Deferred Object In Twisted

阅读更多
Twisted uses the Deferred object to manage the callback sequence. The client application attaches a series of functions to the deferred to be called in order when the results of the asychronous request are available(this series of functions is known as a series of callbacks, or a callback chain), together with a series of functions to be called if there is an error in the asychronous request(known as a series of errbacks or an errback chain). The asychronous library code calls the first callback when the result is available, or the first errback when an error occurs, and the Deferred object then hands the results of each callback or errback function to the next function in the chain.

 

The Problem that Deferreds Solve

It is the second class of concurrency problem - non-computationally intensive tasks that involve an appreciable delay - that Deferreds are designed to help solve. Functions that wait on hard drive access, database access, and network access all fall into this class, although the time delay varies.

Deferreds are designed to enable Twisted programs to wait for data without hanging until that data arrives. They do this by giving a simple management interface for callbacks to libraries and applications.  Libraries know that they always make their results available by calling Deferred.callback and error by calling Deferred.errback. Applications set up result handlers by attaching callbacks and errbacks to deferreds in the order they want them called.



The basic idea behind Deferreds, and other solutions to this problem, is to keep the CPU as active as possible. If one task is waiting on data, rather than have the CPU(and the program!) idle waiting for that data(a process normally called "blocking"), the program performs other operations in the meantime, and waits for some singnal that data is ready to be processed before returning to that process.

In Twisted, a function signals to the calling function that it is waiting by returning a Deferred. When the data is available, the program activeates the callbacks on that Deferred to process the data.



Deferreds - a signal that data is yet to come

In our email sending example above, a parent functions calls a function to connect to the remote server. Asynchrony requires that this connection function return without waiting for the result so that the parent function can do other things. So how does the parent function or its controlling program know that the connection doesn't exist yet, and how does it use the connection once it does exist?

Twisted has an object that signals this situation. When the connection function returns, it signals that the operation is imcomplete by returning a twisted.internet.defer.Deferred object.

The Deferred has two purposes. The first is that it says "I am a signal that the result of whatever you wanted me to do is still pending.". The second is that you can ask the Deferred to run things when the data does arrive.



Callbacks

The way you tell a Deferred what to do with the data once it arrives is by adding a callback - asking the Deferred to call a function once the data arrives.

One Twisted library function that returns a Deferred is twisted.web.client.getPage. In this example, we call getPage, which returns a Deferred, and we attach a callback to handle the contents of the page once the data is available:

from twisted.web.client import getPage
from twisted.internet import reactor

def printContents(contents):
    '''
    This is the 'callback' function, added to the Deferred and called by it when the promised data is available
    '''

    print "The Deferred has called printContents with the following contents:"
    print contents 

    #Stop the Twisted event handling system -- this is usually handled
    #in higher lever ways

    reactor.stop() 

   #call getPage, which returns immediately with a Deferred, promissing to
   #pass the page contents onto our callbacks when the contents are available

deferred = getPage('http://www.google.com')

#added a callback to the deferred -- request that it run printContents when
#the page content has been downloaded

deferred.addCallback(printContents)

#Begin the Twisted event handling system to manage the process -- again
# this isn't the usual way to do this

reactor.run()



A very common use of Deferreds is to attach two callbacks. The result of the first callback is passed to the second callback:


from twisted.web.client import getPage
from twisted.internet import reactor

def lowerCaseContents(contents):
     '''
     This is a 'callback' function, added to the Deferred and called by
     it when the promised data is available. It converts all the data to lower
     case.
     '''

     return contents.lower()

def printContents(contents):

    '''
    This a 'callback' function, added to the Deferred after lowerCaseContents
    and called by it with the results of lowerCaseContents
    '''
    print contents
    reactor.stop()

deferred = getPage('http://www.google.com')

#add two callbacks to the deferred -- request that it run lowerCaseContents
#when the page content has been downloaded, and then run printContents with
#the result of lowerCaseContents

deferred.addCallback(lowerCaseContents)

deferred.addCallback(printContents)

reactor.run()


Error handling: errbacks

Just as a asynchronous function returns before its result is available, it may also return before it is possible to detect errors: failed connections, erroneous data, protocol errors, and so on. Just as you can add callbacks to a Deferred which it calls when the data you are expecting is available, you can add error handlers(errbacks) to a Deferred for it to call when an error occurs and it cannot obtain the data:


from twisted.web.client import getPage
from twisted.internet import reactor

def errorHandler(err):
    '''
    This is an 'errback' function, added to the Deferred which will call it in 
    the event of an error
     '''
    #this isn't a very effective handling of the error, we just print it out:
 
    print "An error has occurred: <%s>" % str(err)
    #and then we stop the entire process:
    reactor.stop()

def printContents(contents):
    '''
    This is a 'callback' function, added to the Deferred and called by it which 
    the page content
    '''
    print contents
    reactor.stop()

#we request a page which doesn't exist in order to demonstrate the
#error chain

deferred = getPage("http://www.google.com/does-not-exits")

#add the callback to the Deferred to handle the page content

deferred.addCallback(printContents)

#add the errback to the Deferred to handle any errors

deferred.addErrback(errorHandler)

reactor.run()


分享到:
评论

相关推荐

    android-deferred-object, 在 jQuery http上,Android实现延迟对象 Pattern的实现.zip

    android-deferred-object, 在 jQuery http上,Android实现延迟对象 Pattern的实现 Android延迟对象Android-Deferred-Object是一个of实用工具对象,可以以将多个回调 register 调回回调队列,调用回调队列和异步功能...

    Twisted和Scrapy

    **Twisted和Scrapy是两个在Python编程领域中非常重要的框架,它们分别专注于网络异步I/O和网络爬虫开发。** **Twisted** 是一个开源的Python库,主要用于实现异步网络编程。它提供了丰富的网络协议支持,包括TCP、...

    Android代码-jdeferred

    JDeferred is a Java Deferred/Promise library similar to JQuery's Deferred Object. Inspired by JQuery and Android Deferred Object. Features Deferred object and Promise Promise callbacks .then(…) ....

    中文Twisted入门教程

    通过对Reactor模型和Deferred机制的学习,开发者可以更好地理解和掌握Twisted的核心思想,进而有效地利用Twisted解决实际问题。无论是对于初学者还是经验丰富的开发者来说,Twisted都提供了丰富的工具和API,帮助...

    Twisted系列教程 的中文翻译

    在第七部分到第九部分中,教程引入了Deferred对象,这是Twisted框架中用于管理异步操作结果的一种机制。Deferred对象可以将回调函数的书写模式进一步抽象,使得异步编程更加简洁明了。这部分详细地讲述了Deferred的...

    Twisted事件驱动网络框架

    `Deferred`是Twisted中处理异步操作的关键抽象。它是一个承诺,表示在未来某个时间点将提供一个结果。通过链式调用`addCallback`和`addErrback`方法,可以注册回调函数来处理成功的结果或错误情况,这样可以编写出...

    Twisted系列教程.pdf

    - **Deferred**:这部分详细介绍了Deferred对象的功能及其在Twisted中的作用,包括如何使用它们来组织异步操作的执行顺序。 #### 第八至第十八部分:从使用Deferred的诗歌下载客户端到Deferreds全貌 - **使用...

    对最基础的twisted架构进行了介绍,其中重点讲解了reactor和deferred

    本文将深入探讨Twisted的基础架构,重点关注其核心组件——Reactor和Deferred,帮助读者建立起对Twisted核心概念的深刻理解。 首先,让我们了解什么是Reactor。在Twisted中,Reactor扮演着事件循环的角色,它是整个...

    twisted 异步教程 中文版

    本教程深入介绍了Twisted的理论基础、异步编程模式与Reactor的初探、Twisted支持的客户端与服务器的开发以及Deferred对象的使用。Deferred是Twisted框架中用于处理异步操作完成后的回调函数的对象,它能够帮助开发者...

    Python的Twisted框架中使用Deferred对象来管理回调函数

    在Python的Twisted框架中,异步编程是通过 Deferred 对象来实现的,它是一种强大的工具,用于管理回调函数,特别是在处理非阻塞I/O和异步操作时。 Deferred 对象的核心在于它能够将多个回调函数组织成一个执行序列,...

    实例解析Python的Twisted框架中Deferred对象的用法

    Deferred对象结构 Deferred由一系列成对的回调链组成,每一对都包含一个用于处理成功的回调(callbacks)和...from twisted.internet.defer import Deferred def myCallback(result): print result d = Deferred() d.a

    Twisted与异步编程入门

    - **Twisted的异步编程特点**:介绍Twisted中实现异步编程的关键技术,比如`Deferred`对象和回调链等。 #### 初识Twisted 在这一部分,文档开始引导读者实际接触Twisted的基本用法。 - **Twisted的安装与配置**:...

    twisted例子

    5. **延迟调用与定时器**:Twisted提供了延迟调用(Deferred)机制,用于处理异步操作的结果。示例可能演示如何使用延迟调用来处理未来的操作。 6. **认证与加密**:Twisted包含了多种安全协议,如SSL/TLS加密,...

    Decalicious – Deferred Decal System v1.5

    Limit Decal to Game Object: Choose whether a Decal should be drawn on every surface in its bounds, or be restricted to a specific GameObject's surfaces. Works with Terrain: Add detail to your ...

    python twisted 网络编程教程 有代码示例

    `Deferred` 是 Twisted 中的一个关键概念,用于处理异步操作的结果。它们代表了一个将来可能可用的结果,并允许开发者注册回调函数来处理这些结果。这使得编写异步代码变得非常直观,类似于同步代码的写法。 ######...

    适合python3.8的Twisted

    7. **Concurrency**: 除了传统的回调机制,`Twisted` 还引入了协程(`Deferred`)的概念,使得异步编程更加直观和易于管理。 8. **Testing**: `Twisted` 内置了测试框架` Trial`,用于编写和执行单元测试,确保代码的...

    谈谈jQuery之Deferred源码剖析

    ### jQuery之Deferred源码剖析知识点 #### 一、Deferred和Promise简介 jQuery的Deferred对象是jQuery在ES6的Promise概念出现之前的一种实现,它允许开发者将异步操作的结果通过链式调用的方式处理,避免了传统的...

Global site tag (gtag.js) - Google Analytics