suasync包中Deferred类API及应用示例

suasync的Deferred类提供了一种管理异步处理和动态处理链的简洁优雅的方式,类似于带有动态回调链的Future。当Deferred的结果可用时,会触发回调链。错误处理通过errback进行,允许构建复杂的异步API。本文深入介绍了 Deferred 的概念、回调链的工作原理以及错误处理策略。
摘要由CSDN通过智能技术生成
public final class Deferred<T>  extends Object

A thread-safeimplementation of a deferred result for easy asynchronous processing.

Thisimplementation is based on Twisted's Python DeferredAPI.

This API is a simple and elegant way ofmanaging asynchronous and dynamic "pipelines" (processing chains)without having to explicitly define a finite state machine.

 

The tl;dr version

We're all busy and don't always have timeto RTFM in details. Please pay special attention to theinvariants youmust respect. Other than that, here's an executive summary of whatDeferredoffers:

  • A Deferred is like a Future with a dynamic Callback chain associated to it.
  • When the deferred result becomes available, the callback chain gets triggered.
  • The result of one callback in the chain is passed on to the next.
  • When a callback returns another Deferred, the next callback in the chain doesn't get executed until that otherDeferred result becomes available.
  • There are actually two callback chains. One is for normal processing, the other is for error handling / recovery. ACallback that handles errors is called an "errback".
  • Deferred is an important building block for writing easy-to-use asynchronous APIs in a thread-safe fashion.

Understanding the concept of Deferred

The idea is that a Deferredrepresents a result that's not yet available. An asynchronous operation (I/O,RPC, whatever) has been started and will hand its result (be it successful ornot) to theDeferred inthe future. The key difference between a Deferred and aFuture isthat a Deferred has acallback chain associated to it, whereas withjust aFuture youneed get the result manually at some point, which poses problems such as: Howdo you know when the result is available? What if the result itself depends onanother future?

When you start anasynchronous operation, you typically want to be called back when the operationcompletes. If the operation was successful, you want your callback to use itsresult to carry on what you were doing at the time you started the asynchronousoperation. If there was an error, you want to trigger some error handling code.

But there's moreto a Deferred than a single callback. You can addarbitrary number of callbacks, which effectively allows you to easily buildcomplex processing pipelines in a really simple and elegant way.

Understanding the callback chain

Let's take a typical example. You'rewriting a client library for others to use your simple remote storage service.When your users call theget method in your library, you want toretrieve some piece of data from your remote service and hand it back to theuser, but you want to do so in an asynchronous fashion.

When the user ofyour client library invokes get, you assemble arequest and send it out to the remote server through a socket. Before sendingit to the socket, you create aDeferred and you store itsomewhere, for example in a map, to keep an association between the request andthisDeferred. You then return this Deferredto the user, this is how they will access the deferred result as soon as theRPC completes.

Sooner or later,the RPC will complete (successfully or not), and your socket will becomereadable (or maybe closed, in the event of a failur

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值