async 和 await 实现原理

来源:http://blog.csdn.net/cjq1234/article/details/7536644

摘要:用串行运行的状态机模拟实现了异步

 

今天在stackOverflow网站看到一个很好的解释,摘抄并发挥一下,

It works similarly to the yield return keyword in C# 2.0.

An asynchronous method is not actually an ordinary sequential method. It is compiled into a state machine (an object) with some state (local variables are turned into fields of the object). Each block of code between two uses of await is one "step" of the state machine.

This means that when the method starts, it just runs the first step and then the state machine returns and schedules some work to be done - when the work is done, it will run the next step of the state machine. For example this code:

  1. async Task Demo() {  
  2.   var v1 = foo();  
  3.   var v2 = await bar();  
  4.   more(v1, v2);  
  5. }  
async Task Demo() {
  var v1 = foo();
  var v2 = await bar();
  more(v1, v2);
}



Would be translated to something like:

[csharp]
  1. class _Demo {  
  2.   int _v1, _v2;  
  3.   int _state = 0;  
  4.   Task<int> _await1;  
  5.   public void Step() {  
  6.     switch(this._state) {  
  7.     case 0:  
  8.       foo();  
  9.       this._v1 = foo();  
  10.       this._await1 = bar();  
  11.       // When the async operation completes, it will call this method  
  12.       this._state = 1;  
  13.       op.SetContinuation(Step);  
  14.     case 1:  
  15.       this._v2 = this._await1.Result; // Get the result of the operation  
  16.       more(this._v1, this._v2);  
  17.   }  
  18. }  
class _Demo {
  int _v1, _v2;
  int _state = 0;
  Task<int> _await1;
  public void Step() {
    switch(this._state) {
    case 0:
      foo();
      this._v1 = foo();
      this._await1 = bar();
      // When the async operation completes, it will call this method
      this._state = 1;
      op.SetContinuation(Step);
    case 1:
      this._v2 = this._await1.Result; // Get the result of the operation
      more(this._v1, this._v2);
  }
}


The important part is that it just uses the SetContinuation method to specify that when the operation completes, it should call the Step method again (and the method knows that it should run the second bit of the original code using the _state field). You can easily imagine that the SetContinuation would be something like btn.Click += Step, which would run completely on a single thread.

The asynchronous programming model in C# is very close to F# asynchronous workflows (in fact, it is essentially the same thing, aside from some technical details), and writing reactive single-threaded GUI applications using async is quite an interesting area - at least I think so - see for example this article (maybe I should write a C# version now :-)).

The translation is similar to iterators (and yield return) and in fact, it was possible to use iterators to implement asynchronous programming in C# earlier. I wrote an article about that a while ago - and I think it can still give you some insight on how the translation works.

answered by Tomas Petricek

大致是说 async 和await模拟了一个状态机(上面的_state变量维持状态),async把一个顺序执行的工作分割成3块,一个是费时工作的前序,一个是费时的工作,还有一个是费时工作的后续,

[csharp]
  1. async Task Demo() {  
  2.   var v1 = 前序工作();  
  3.   var v2 = await 费时工作();  
  4.   后续工作(v1, v2);  
  5. }  
async Task Demo() {
  var v1 = 前序工作();
  var v2 = await 费时工作();
  后续工作(v1, v2);
}



被翻译成状态机
[csharp]
  1. class 状态机{  
  2.     int _v1, _v2;  
  3.     int 状态变量 =起始态;     
  4.     Task<int> _await1;    
  5.     public void Step() {      
  6.         switch(this.状态变量) {     
  7.             case 起始态:         
  8.                 this._v1 = 前序工作();  
  9.                 this._await1 = 费时工作();        
  10.                 // 当费时工作异步完成时, 异步工作将改变状态变量值  
  11.                 this.状态变量= 顺利完成;        
  12.                 继续调用自身即 Step函数;    //op.SetContinuation(Step);      
  13.             case 顺利完成:        
  14.                 this._v2 = this._await1.Result; //费时工作结果       
  15.                 后续工作(this._v1, this._v2);    
  16.     }  
  17. }  
class 状态机{
    int _v1, _v2;
    int 状态变量 =起始态;   
    Task<int> _await1;  
    public void Step() {    
        switch(this.状态变量) {   
            case 起始态:       
                this._v1 = 前序工作();
                this._await1 = 费时工作();      
                // 当费时工作异步完成时, 异步工作将改变状态变量值
                this.状态变量= 顺利完成;      
                继续调用自身即 Step函数;    //op.SetContinuation(Step);    
            case 顺利完成:      
                this._v2 = this._await1.Result; //费时工作结果     
                后续工作(this._v1, this._v2);  
    }
}



事实上我们亦可以在.net framework 4以下向上面的状态机类一样来调用异步函数,这样同步调用的逻辑还是保持住了,也更好理解,最大的好处是对于循环的处理简单多了。
### Async/Await 的工作原理 `async` `await` 是 JavaScript 中用于简化异步代码的语法糖,它们基于 Promise 构建。以下是关于其核心原理的详细说明: #### 1. **Async 函数** 当一个函数被声明为 `async` 时,它会自动返回一个 Promise 对象[^2]。即使该函数内部没有显式的 `return new Promise()`,它的返回值也会被包装成一个 Promise。 如果函数正常结束并返回某个值,则这个值会被作为 Promise 的 resolved 值;如果抛出了异常,则此异常将成为 Promise 的 rejected 值[^3]。 ```javascript async function example() { return "Hello"; } example().then(value => console.log(value)); // 输出: Hello ``` #### 2. **Await 关键字** `await` 操作符只能在 `async` 函数中使用,用来暂停当前函数的执行,直到指定的 Promise 被 fulfilled 或 rejected。一旦 Promise 结果可用,`await` 将解包该结果并继续执行后续逻辑。 需要注意的是,“暂停”仅限于当前 `async` 函数范围内,并不会阻塞整个事件循环或其他线程上的任务运行。 ```javascript function delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function testDelay() { console.log('Start'); await delay(1000); console.log('End after 1 second'); } testDelay(); // Start -> (等待一秒) -> End after 1 second ``` #### 3. **错误处理机制** 对于可能失败的操作,在使用 `try...catch` 结构包裹下可以捕获由未满足的 Promises 所引发的任何错误[^1]。如果没有设置这样的保护措施,那么未被捕获的 rejection 将传播到调用链之外直至全局环境。 ```javascript async function fetchWithErrorHandling(url){ try{ let response = await fetch(url); if(!response.ok){ throw Error(`HTTP error! status: ${response.status}`); } return await response.json(); }catch(err){ console.error("Fetch failed:", err.message); } } fetchWithErrorHandling('/nonexistent').catch(console.dir); ``` #### 4. **编译器优化与状态机实现** 实际上,每当遇到含有 `await` 的表达式时,JavaScript 引擎并不会真正意义上地停止程序流,而是创建了一个复杂的状态机来管理这些异步步骤之间的过渡过程[^4]。这种设计允许其他非关联的任务得以并发进行而不受干扰。 --- ### 实际应用中的注意事项 尽管 `async/await` 提供了一种更直观的方式来书写异步流程控制结构,但在某些场景下仍需谨慎对待: - 不要滥用多个连续的独立请求串连在一起,因为这可能会降低整体性能; - 明确区分同步部分真正的异步依赖关系,避免不必要的延迟引入。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值