C#中的異步迭代器是什么-創(chuàng)新互聯(lián)

這篇文章給大家介紹C#中的異步迭代器是什么,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)江西,10多年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575

迭代器的概念


迭代器的概念在C#中出現(xiàn)的比較早,很多人可能已經(jīng)比較熟悉了。

通常迭代器會用在一些特定的場景中。

舉個例子:有一個foreach循環(huán):

foreach (var item in Sources)
{
  Console.WriteLine(item);
}

這個循環(huán)實現(xiàn)了一個簡單的功能:把Sources中的每一項在控制臺中打印出來。

有時候,Sources可能會是一組完全緩存的數(shù)據(jù),例如:List<string>:

IEnumerable<string> Sources(int x)
{
  var list = new List<string>();
  for (int i = 0; i < 5; i++)
    list.Add($"result from Sources, x={x}, result {i}");
  return list;
}

這里會有一個小問題:在我們打印Sources的第一個的數(shù)據(jù)之前,要先運行完整運行Sources()方法來準(zhǔn)備數(shù)據(jù),在實際應(yīng)用中,這可能會花費大量時間和內(nèi)存。更有甚者,Sources可能是一個無邊界的列表,或者不定長的開放式列表,比方一次只處理一個數(shù)據(jù)項目的隊列,或者本身沒有邏輯結(jié)束的隊列。

這種情況,C#給出了一個很好的迭代器解決:

IEnumerable<string> Sources(int x)
{
  for (int i = 0; i < 5; i++)
    yield return $"result from Sources, x={x}, result {i}";
}

這個方式的工作原理與上一段代碼很像,但有一些根本的區(qū)別 - 我們沒有用緩存,而只是每次讓一個元素可用。

為了幫助理解,來看看foreach在編譯器中的解釋:

using (var iter = Sources.GetEnumerator())
{
  while (iter.MoveNext())
  {
    var item = iter.Current;
    Console.WriteLine(item);
  }
}

當(dāng)然,這個是省略掉很多東西后的概念解釋,我們不糾結(jié)這個細(xì)節(jié)。但大體的意思是這樣的:編譯器對傳遞給foreach的表達(dá)式調(diào)用GetEnumerator(),然后用一個循環(huán)去檢查是否有下一個數(shù)據(jù)(MoveNext()),在得到肯定答案后,前進(jìn)并訪問Current屬性。而這個屬性代表了前進(jìn)到的元素。?

上面這個例子,我們通過MoveNext()/Current方式訪問了一個沒有大小限制的向前的列表。我們還用到了yield迭代器這個很復(fù)雜的東西 - 至少我是這么認(rèn)為的。

我們把上面的例子中的yield去掉,改寫一下看看:

IEnumerable<string> Sources(int x) => new GeneratedEnumerable(x);

class GeneratedEnumerable : IEnumerable<string>
{
  private int x;
  public GeneratedEnumerable(int x) => this.x = x;

  public IEnumerator<string> GetEnumerator() => new GeneratedEnumerator(x);

  IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

class GeneratedEnumerator : IEnumerator<string>
{
  private int x, i;
  public GeneratedEnumerator(int x) => this.x = x;

  public string Current { get; private set; }

  object IEnumerator.Current => Current;

  public void Dispose() { }

  public bool MoveNext()
  {
    if (i < 5)
    {
      Current = $"result from Sources, x={x}, result {i}";
      i++;
      return true;
    }
    else
    {
      return false;
    }
  }

  void IEnumerator.Reset() => throw new NotSupportedException();
}

這樣寫完,對照上面的yield迭代器,理解工作過程就比較容易了:

首先,我們給出一個對象IEnumerable。注意,IEnumerable和IEnumerator是不同的。

當(dāng)我們調(diào)用Sources時,就創(chuàng)建了GeneratedEnumerable。它存儲狀態(tài)參數(shù)x,并公開了需要的IEnumerable方法。

后面,在需要foreach迭代數(shù)據(jù)時,會調(diào)用GetEnumerator(),而它又調(diào)用GeneratedEnumerator以充當(dāng)數(shù)據(jù)上的游標(biāo)。

MoveNext()方法邏輯上實現(xiàn)了for循環(huán),只不過,每次調(diào)用MoveNext()只執(zhí)行一步。更多的數(shù)據(jù)會通過Current回傳過來。另外補充一點:MoveNext()方法中的return false對應(yīng)于yield break關(guān)鍵字,用于終止迭代。

是不是好理解了?

下面說說異步中的迭代器。

異步中的迭代器

上面的迭代,是同步的過程。而現(xiàn)在Dotnet開發(fā)工作更傾向于異步,使用async/await來做,特別是在提高服務(wù)器的可伸縮性方面應(yīng)用特別多。

上面的代碼較大的問題,在于MoveNext()。很明顯,這是個同步的方法。如果它運行需要一段時間,那線程就會被阻塞。這會讓代碼執(zhí)行過程變得不可接受。

我們能做得最接近的方法是異步獲取數(shù)據(jù):

async Task<List<string>> Sources(int x) {...}

但是,異步獲取數(shù)據(jù)并不能解決數(shù)據(jù)緩存延遲的問題。

好在,C#為此特意增加了對異步迭代器的支持:

public interface IAsyncEnumerable<out T>
{
  IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default);
}
public interface IAsyncEnumerator<out T> : IAsyncDisposable
{
  T Current { get; }
  ValueTask<bool> MoveNextAsync();
}
public interface IAsyncDisposable
{
  ValueTask DisposeAsync();
}

注意,從.NET Standard 2.1和.NET Core 3.0開始,異步迭代器已經(jīng)包含在框架中了。而在早期版本中,需要手動引入:

# dotnet add package Microsoft.Bcl.AsyncInterfaces
目前這個包的版本號是5.0.0。

還是上面例子的邏輯:

IAsyncEnumerable<string> Source(int x) => throw new NotImplementedException();

看看foreach可以await后的樣子:

await foreach (var item in Sources)
{
  Console.WriteLine(item);
}

編譯器會將它解釋為:

await using (var iter = Sources.GetAsyncEnumerator())
{
  while (await iter.MoveNextAsync())
  {
    var item = iter.Current;
    Console.WriteLine(item);
  }
}

這兒有個新東西:await using。與using用法相同,但釋放時會調(diào)用DisposeAsync,而不是Dispose,包括回收清理也是異步的。

這段代碼其實跟前邊的同步版本非常相似,只是增加了await。但是,編譯器會分解并重寫異步狀態(tài)機,它就變成異步的了。原理不細(xì)說了,不是本文關(guān)注的內(nèi)容。

那么,帶有yield的迭代器如何異步呢?看代碼:

async IAsyncEnumerable<string> Sources(int x)
{
  for (int i = 0; i < 5; i++)
  {
    await Task.Delay(100); // 這兒模擬異步延遲
    yield return $"result from Sources, x={x}, result {i}";
  }
}

嗯,看著就舒服。

這就完了?圖樣圖森破。異步有一個很重要的特性:取消。

那么,怎么取消異步迭代?

異步迭代的取消

異步方法通過CancellationToken來支持取消。異步迭代也不例外??纯瓷厦鍵AsyncEnumerator<T>的定義,取消標(biāo)志也被傳遞到了GetAsyncEnumerator()方法中。

那么,如果是手工循環(huán)呢?我們可以這樣寫:

await foreach (var item in Sources.WithCancellation(cancellationToken).ConfigureAwait(false))
{
  Console.WriteLine(item);
}

這個寫法等同于:

var iter = Sources.GetAsyncEnumerator(cancellationToken);
await using (iter.ConfigureAwait(false))
{
  while (await iter.MoveNextAsync().ConfigureAwait(false))
  {
    var item = iter.Current;
    Console.WriteLine(item);
  }
}

沒錯,ConfigureAwait也適用于DisposeAsync()。所以最后就變成了:

await iter.DisposeAsync().ConfigureAwait(false);

異步迭代的取消捕獲做完了,接下來怎么用呢?

看代碼:

IAsyncEnumerable<string> Sources(int x) => new SourcesEnumerable(x);
class SourcesEnumerable : IAsyncEnumerable<string>
{
  private int x;
  public SourcesEnumerable(int x) => this.x = x;

  public async IAsyncEnumerator<string> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  {
    for (int i = 0; i < 5; i++)
    {
      await Task.Delay(100, cancellationToken); // 模擬異步延遲
      yield return $"result from Sources, x={x}, result {i}";
    }
  }
}

如果有CancellationToken通過WithCancellation傳過來,迭代器會在正確的時間被取消 - 包括異步獲取數(shù)據(jù)期間(例子中的Task.Delay期間)。當(dāng)然我們還可以在迭代器中任何一個位置檢查IsCancellationRequested或調(diào)用ThrowIfCancellationRequested()。

此外,編譯器也會通過[EnumeratorCancellation]來完成這個任務(wù),所以我們還可以這樣寫:

async IAsyncEnumerable<string> Sources(int x, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
  for (int i = 0; i < 5; i++)
  {
    await Task.Delay(100, cancellationToken); // 模擬異步延遲
    yield return $"result from Sources, x={x}, result {i}";
  }
}

這個寫法與上面的代碼其實是一樣的,區(qū)別在于加了一個參數(shù)。

實際應(yīng)用中,我們有下面幾種寫法上的選擇:

// 不取消
await foreach (var item in Sources)

// 通過WithCancellation取消
await foreach (var item in Sources.WithCancellation(cancellationToken))

// 通過SourcesAsync取消
await foreach (var item in SourcesAsync(cancellationToken))

// 通過SourcesAsync和WithCancellation取消
await foreach (var item in SourcesAsync(cancellationToken).WithCancellation(cancellationToken))

// 通過不同的Token取消
await foreach (var item in SourcesAsync(tokenA).WithCancellation(tokenB))

幾種方式區(qū)別于應(yīng)用場景,實質(zhì)上沒有區(qū)別。對兩個Token的方式,任何一個Token被取消時,任務(wù)會被取消。

關(guān)于C#中的異步迭代器是什么就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網(wǎng)頁標(biāo)題:C#中的異步迭代器是什么-創(chuàng)新互聯(lián)
文章位置:http://muchs.cn/article24/coccje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、網(wǎng)站策劃、外貿(mào)建站、移動網(wǎng)站建設(shè)、網(wǎng)站收錄、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司