1. 程式人生 > >async 和 await 實現原理

async 和 await 實現原理

摘要:用序列執行的狀態機模擬實現了非同步

今天在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:

[csharp] view plaincopyprint?
  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.   publicvoid 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.     publicvoid 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以下向上面的狀態機類一樣來呼叫非同步函式,這樣同步呼叫的邏輯還是保持住了,也更好理解,最大的好處是對於迴圈的處理簡單多了。