- 浏览: 333374 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
ProgrammingPower:
非常感谢您,搞定了,哈哈!
Debian下安装CodeBlocks -
hfa1s2:
是的。谢谢你哈! 我解决了这个问题
python模块之smtplib: 用python发送SSL/TLS安全邮件 -
huangro:
587端口的是很麻烦的,毕竟是需要ssl验证的。
python模块之smtplib: 用python发送SSL/TLS安全邮件 -
hfa1s2:
我想利用内网往公司邮箱里发,但接口是587 的 而且需要 SS ...
python模块之smtplib: 用python发送SSL/TLS安全邮件 -
zyb88325:
你好 我现在用的cakephp2.0,因为服务器不支持rewr ...
CakePHP常用技巧总结
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:
A very common use of Deferreds is to attach two callbacks. The result of the first callback is passed to the second callback:
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:
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()
发表评论
-
Trac Data Models and Schema
2010-09-15 10:56 6355The main models that developers ... -
在Mac OS上运行wxPython
2010-09-14 15:42 3215如果在一个64位的Mac上跑wxPython, 将会出现以下错 ... -
Trac SQL Database API
2010-09-14 13:41 1044Trac SQL Database API Trac use ... -
Trac系统相关文档
2010-02-01 12:53 10561. Trac组件架构 http://trac.edgewal ... -
Django上传文件
2009-11-26 18:06 1355之前写了一个上传文件模块,但是当文件很大时就会挂掉,分析了一下 ... -
50个Python重要模块
2009-10-20 09:32 1987原文地址:http://www.cnblogs.com/yd1 ... -
Trac系统的双重认证
2009-10-20 09:27 1141首先,需要导入urllib2及cookielib。 然后,关键 ... -
分享一个图表制作库
2009-06-18 15:15 1314该包名为ChartDirector,可以从其官方主页http: ... -
django newforms的更改
2009-06-16 13:34 1058Django升级之后,有一些应用包的名称也相应发生了变化,比如 ... -
Python版支付宝集成插件源代码
2009-05-08 15:34 4919之前在做一项目的时候需要集成支付宝功能,网站是用python做 ... -
Django多语言问题
2009-05-06 17:22 1910最近多Django比较感兴趣,打算用它来做一个网站,看了官方关 ... -
Installing Satchmo on Webfaction
2009-04-29 15:39 1654link: http://brianmckinney.net/ ... -
Satchmo配置注意事项
2009-04-25 16:10 1027Satchmo在同步数据库之后,在操作后台管理页面过程中,一直 ... -
Yaml与python类型的对照表
2009-04-22 23:03 1699因为常用yaml作为python程序的配置文件,特将其与pyt ... -
satchmo汉化
2009-04-21 15:21 1905转载:http://blog.csdn.net/huliuhe ... -
python模块之smtplib: 用python发送SSL/TLS安全邮件
2008-07-04 19:08 9460原文出自 http://blog.csdn.net/zhaow ... -
python模块之poplib: 用pop3收取邮件
2008-07-04 19:09 2204python的poplib模块是用来 ... -
python模块之email: 电子邮件编码解码 (转)
2008-07-04 19:11 3502用email模块来生成邮件也是很简单的,只是需要一些mime的 ... -
用iconv做通用的语言编码转换(转)
2008-07-04 19:13 1375#include <stdio.h> #in ... -
python模块之email: 电子邮件编码解码(转)
2008-07-04 19:16 3284出处:http://blog.csdn.net/zhaowei ...
相关推荐
### 实例解析Python的Twisted框架中Deferred对象的用法 #### 一、Deferred对象概述 在探讨Deferred对象之前,我们需要了解Twisted框架的核心特点之一就是它的异步性。Twisted是一个事件驱动的网络引擎,它通过非...
在Python的Twisted框架中,`Deferred`对象是异步编程的核心组件,它主要用于处理回调,使得开发者能够优雅地处理异步操作的结果。在Twisted这样的事件驱动网络库中,`Deferred`对象扮演着关键角色,因为它允许你注册...
labview程序代码参考学习使用,希望对你有所帮助。
毕设和企业适用springboot生鲜鲜花类及数据处理平台源码+论文+视频.zip
毕设和企业适用springboot企业数据智能分析平台类及汽车管理平台源码+论文+视频
毕设和企业适用springboot社区物业类及企业创新研发平台源码+论文+视频
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Floating Text Example</title> <style> .floating-text { font-size: 24px; position: relative; animation: float 3s ease-in-out infinite; } @keyframes float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-20px); } } </style> </head> <body> <div class="floating-text">Hello, I'm floating!</div> <script> document.addEventListener('DOMContentLoaded', function() {
毕设和企业适用springboot社交媒体分析平台类及智慧医疗管理平台源码+论文+视频
毕设和企业适用springboot生鲜鲜花类及餐饮管理平台源码+论文+视频
毕设和企业适用springboot人工智能客服系统类及用户行为分析平台源码+论文+视频
毕设和企业适用springboot全渠道电商平台类及个性化广告平台源码+论文+视频
毕设和企业适用springboot社交互动平台类及线上图书馆源码+论文+视频
毕设和企业适用springboot企业知识管理平台类及供应链优化平台源码+论文+视频
毕设和企业适用springboot企业健康管理平台类及数据处理平台源码+论文+视频.zip
内容概要:本文档是一份面向初学者的详细指南,重点介绍如何利用Vue.js 2.0快速创建和运行简单的Todo List应用。首先指导安装必需的Node.js、npm/yarn等环境准备,接着通过Vue CLI工具生成新的Vue项目,再详细介绍项目目录和组件的构建方式。最后提供了具体的方法实现添加和删除待办事项,并指导如何使用命令启动应用,查看结果。 适合人群:具备基础Web开发技能的前端开发新手,尤其是对Vue框架感兴趣的学习者。 使用场景及目标:作为初学者入门级的学习资料,本文档的目标是让读者能够在最短时间内掌握Vue.js的基础概念和技术栈的应用方式,以便日后可以独立地构建更加复杂的Vue应用。 其他说明:除了学习如何构建应用程序之外,本文档还涵盖了Vue的基本语法和数据绑定、事件处理机制等重要概念,对于理解Vue框架的工作原理十分有帮助。
毕设和企业适用springboot企业健康管理平台类及智能化系统源码+论文+视频.zip
毕设和企业适用springboot企业健康管理平台类及远程医疗平台源码+论文+视频.zip
毕设和企业适用springboot数据可视化类及数据智能化平台源码+论文+视频