欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

聊一聊C# 8.0中的await foreach使用

 更新時間:2019年06月05日 11:58:52   作者:碼農(nóng)阿宇  
這篇文章主要介紹了聊一聊C# 8.0中的await foreach使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

AsyncStreamsInCShaper8.0

很開心今天能與大家一起聊聊C# 8.0中的新特性-Async Streams,一般人通??吹竭@個詞表情是這樣.

簡單說,其實就是C# 8.0中支持await foreach.

或者說,C# 8.0中支持異步返回枚舉類型async Task<IEnumerable<T>>.

好吧,還不懂?Good,這篇文章就是為你寫的,看完這篇文章,你就能明白它的神奇之處了.

為什么寫這篇文章

Async Streams這個功能已經(jīng)發(fā)布很久了,在去年的Build 2018 The future of C#就有演示,最近VS 2019發(fā)布,在該版本的Release Notes中,我再次看到了這個新特性,因為對異步編程不太熟悉,所以借著這個機會,學(xué)習(xí)新特性的同時,把異步編程重溫一遍.

本文內(nèi)容,參考了Bassam Alugili在InfoQ中發(fā)表的Async Streams in C# 8,撰寫本博客前我已聯(lián)系上該作者并得到他支持.

Async / Await

C# 5 引入了 Async/Await,用以提高用戶界面響應(yīng)能力和對 Web 資源的訪問能力。換句話說,異步方法用于執(zhí)行不阻塞線程并返回一個標(biāo)量結(jié)果的異步操作。

微軟多次嘗試簡化異步操作,因為 Async/Await 模式易于理解,所以在開發(fā)人員當(dāng)中獲得了良好的認(rèn)可。

詳見The Task asynchronous programming model in C#

常規(guī)示例

要了解問什么需要Async Streams,我們先來看看這樣的一個示例,求出5以內(nèi)的整數(shù)的和.

static int SumFromOneToCount(int count)
    {
      ConsoleExt.WriteLine("SumFromOneToCount called!");

      var sum = 0;
      for (var i = 0; i <= count; i++)
      {
        sum = sum + i;
      }
      return sum;
    }

調(diào)用方法.

static void Main(string[] args)
    {
      const int count = 5;
      ConsoleExt.WriteLine($"Starting the application with count: {count}!");
      ConsoleExt.WriteLine("Classic sum starting.");
      ConsoleExt.WriteLine($"Classic sum result: {SumFromOneToCount(count)}");
      ConsoleExt.WriteLine("Classic sum completed.");
      ConsoleExt.WriteLine("################################################");
    }

輸出結(jié)果.

可以看到,整個過程就一個線程Id為1的線程自上而下執(zhí)行,這是最基礎(chǔ)的做法.

Yield Return

接下來,我們使用yield運算符使得這個方法編程延遲加載,如下所示.

static IEnumerable<int> SumFromOneToCountYield(int count)
    {
      ConsoleExt.WriteLine("SumFromOneToCountYield called!");

      var sum = 0;
      for (var i = 0; i <= count; i++)
      {
        sum = sum + i;

        yield return sum;
      }
    }

主函數(shù)

static void Main(string[] args)
    {
      const int count = 5;
      ConsoleExt.WriteLine("Sum with yield starting.");
      foreach (var i in SumFromOneToCountYield(count))
      {
        ConsoleExt.WriteLine($"Yield sum: {i}");
      }
      ConsoleExt.WriteLine("Sum with yield completed.");

      ConsoleExt.WriteLine("################################################");
      ConsoleExt.WriteLine(Environment.NewLine);
    }

運行結(jié)果如下.

正如你在輸出窗口中看到的那樣,結(jié)果被分成幾個部分返回,而不是作為一個值返回。以上顯示的累積結(jié)果被稱為惰性枚舉。但是,仍然存在一個問題,即 sum 方法阻塞了代碼的執(zhí)行。如果你查看線程ID,可以看到所有東西都在主線程1中運行,這顯然不完美,繼續(xù)改造.

Async Return

我們試著將async用于SumFromOneToCount方法(沒有yield關(guān)鍵字).

static async Task<int> SumFromOneToCountAsync(int count)
    {
      ConsoleExt.WriteLine("SumFromOneToCountAsync called!");

      var result = await Task.Run(() =>
      {
        var sum = 0;

        for (var i = 0; i <= count; i++)
        {
          sum = sum + i;
        }
        return sum;
      });

      return result;
    }

主函數(shù).

static async Task Main(string[] args)
    {
      const int count = 5;
      ConsoleExt.WriteLine("async example starting.");
      // Sum runs asynchronously! Not enough. We need sum to be async with lazy behavior.
      var result = await SumFromOneToCountAsync(count);
      ConsoleExt.WriteLine("async Result: " + result);
      ConsoleExt.WriteLine("async completed.");

      ConsoleExt.WriteLine("################################################");
      ConsoleExt.WriteLine(Environment.NewLine);
    }

運行結(jié)果.

我們可以看到計算過程是在另一個線程中運行,但結(jié)果仍然是作為一個值返回!任然不完美.

如果我們想把惰性枚舉(yield return)與異步方法結(jié)合起來,即返回Task<IEnumerable,這怎么實現(xiàn)呢?

Task<IEnumerable>

我們根據(jù)假設(shè)把代碼改造一遍,使用Task<IEnumerable<T>>來進(jìn)行計算.

可以看到,直接出現(xiàn)錯誤.

IAsyncEnumerable

其實,在C# 8.0中Task<IEnumerable>這種組合稱為IAsyncEnumerable。這個新功能為我們提供了一種很好的技術(shù)來解決拉異步延遲加載的問題,例如從網(wǎng)站下載數(shù)據(jù)或從文件或數(shù)據(jù)庫中讀取記錄,與 IEnumerable 和 IEnumerator 類似,Async Streams 提供了兩個新接口 IAsyncEnumerable 和 IAsyncEnumerator,定義如下:

public interface IAsyncEnumerable<out T>
  {
    IAsyncEnumerator<T> GetAsyncEnumerator();
  }

  public interface IAsyncEnumerator<out T> : IAsyncDisposable
  {
    Task<bool> MoveNextAsync();
    T Current { get; }
  }

  // Async Streams Feature 可以被異步銷毀 
  public interface IAsyncDisposable
  {
   Task DiskposeAsync();
  }

AsyncStream

下面,我們就來見識一下AsyncStrema的威力,我們使用IAsyncEnumerable來對函數(shù)進(jìn)行改造,如下.

static async Task ConsumeAsyncSumSeqeunc(IAsyncEnumerable<int> sequence)
    {
      ConsoleExt.WriteLineAsync("ConsumeAsyncSumSeqeunc Called");

      await foreach (var value in sequence)
      {
        ConsoleExt.WriteLineAsync($"Consuming the value: {value}");

        // simulate some delay!
        await Task.Delay(TimeSpan.FromSeconds(1));
      };
    }

    private static async IAsyncEnumerable<int> ProduceAsyncSumSeqeunc(int count)
    {
      ConsoleExt.WriteLineAsync("ProduceAsyncSumSeqeunc Called");
      var sum = 0;

      for (var i = 0; i <= count; i++)
      {
        sum = sum + i;

        // simulate some delay!
        await Task.Delay(TimeSpan.FromSeconds(0.5));

        yield return sum;
      }
    }

主函數(shù).

 static async Task Main(string[] args)
    {
      const int count = 5;
      ConsoleExt.WriteLine("Starting Async Streams Demo!");

      // Start a new task. Used to produce async sequence of data!
      IAsyncEnumerable<int> pullBasedAsyncSequence = ProduceAsyncSumSeqeunc(count);

      // Start another task; Used to consume the async data sequence!
      var consumingTask = Task.Run(() => ConsumeAsyncSumSeqeunc(pullBasedAsyncSequence));

      await Task.Delay(TimeSpan.FromSeconds(3));

      ConsoleExt.WriteLineAsync("X#X#X#X#X#X#X#X#X#X# Doing some other work X#X#X#X#X#X#X#X#X#X#");

      // Just for demo! Wait until the task is finished!
      await consumingTask;

      ConsoleExt.WriteLineAsync("Async Streams Demo Done!");
    }

如果一切順利,那么就能看到這樣的運行結(jié)果了.

最后,看到這就是我們想要的結(jié)果,在枚舉的基礎(chǔ)上,進(jìn)行了異步迭代.

可以看到,整個計算過程并沒有造成主線程的阻塞,其中,值得重點關(guān)注的是紅色方框區(qū)域的線程5!線程5!線程5!線程5在請求下一個結(jié)果后,并沒有等待結(jié)果返回,而是去了Main()函數(shù)中做了別的事情,等待請求的結(jié)果返回后,線程5又接著執(zhí)行foreach中任務(wù).

Client/Server的異步拉取

如果還沒有理解Async Streams的好處,那么我借助客戶端 / 服務(wù)器端架構(gòu)是演示這一功能優(yōu)勢的絕佳方法。

同步調(diào)用

客戶端向服務(wù)器端發(fā)送請求,客戶端必須等待(客戶端被阻塞),直到服務(wù)器端做出響應(yīng).

示例中Yield Return就是以這種方式執(zhí)行的,所以整個過程只有一個線程即線程1在處理.

異步調(diào)用

客戶端發(fā)出數(shù)據(jù)塊請求,然后繼續(xù)執(zhí)行其他操作。一旦數(shù)據(jù)塊到達(dá),客戶端就處理接收到的數(shù)據(jù)塊并詢問下一個數(shù)據(jù)塊,依此類推,直到達(dá)到最后一個數(shù)據(jù)塊為止。這正是 Async Streams 想法的來源。

最后一個示例就是以這種方式執(zhí)行的,線程5詢問下一個數(shù)據(jù)后并沒有等待結(jié)果返回,而是去做了Main()函數(shù)中的別的事情,數(shù)據(jù)到達(dá)后,線程5又繼續(xù)處理foreach中的任務(wù).

Tips

如果你使用的是.net core 2.2及以下版本,會遇到這樣的報錯.

需要安裝.net core 3.0 preview的SDK(截至至博客撰寫日期4月9日,.net core SDK最新版本為3.0.100-preview3-010431),安裝好SDK后,如果你是VS 2019正式版,可能無法選擇3.0的與預(yù)覽版,聽過只有VS 2019 Preview才支持.Net core 3.0的預(yù)覽版.

總結(jié)

我們已經(jīng)討論過 Async Streams,它是一種出色的異步拉取技術(shù),可用于進(jìn)行生成多個值的異步計算。

Async Streams 背后的編程概念是異步拉取模型。我們請求獲取序列的下一個元素,并最終得到答復(fù)。Async Streams 提供了一種處理異步數(shù)據(jù)源的絕佳方法,希望對大家能夠有所幫助。

文章中涉及的所有代碼已保存在我的GitHub中,請盡情享用!
https://github.com/liuzhenyulive/AsyncStreamsInCShaper8.0

致謝

之前一直感覺國外的大師級開發(fā)者遙不可及甚至高高在上,在遇到Bassam Alugili之后,我才真正感受到技術(shù)交流沒有高低貴賤,正如他對我說的 The most important thing in this world is sharing the knowledge!
 Thank you,I will keep going!!

參考文獻(xiàn): Async Streams in C# 8

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論