Skip to content

非同期イテレーター #254

Closed
Closed

Description

@ufcpp
  • 基本、「await と yield return が混在できるようになった」でいいんだけど
    • どちらも似たような(しかもだいぶコンパイラーが頑張ってる複雑な)コード生成
    • イテレーターかどうかは body 中に yield を含むかどうかで判定
    • 非同期かどうかは async 修飾がついてるかどうかで判定
  • 用途
    • 「ページング」的な奴。巨大なリストのうち、一定数ずつをネットから取ってきて…みたいなやつ
    • Producer-Consumer
  • イテレーターの方と非同期メソッドの方、どっちに書こう?
  • 戻り値は IAsyncEnumerable<T> or IAsyncEnumerator<T> 固定なんだっけ?
  • EnumeratorCancellationAttribute はこっち用?それとも await foreach の方用?
  • パフォーマンス的な懸念の話書く?

Activity

  1. ufcpp commented on May 7, 2019

    @ufcpp
    OwnerAuthor
        static async Task Y()
        {
            await foreach (var x in X())
            {
                Console.Write(x);
            }
            Console.WriteLine();
    
            var cts = new CancellationTokenSource();
            cts.Cancel();
    
            // ↓この WithCancellation で渡したトークンが、GetAsyncEnumerator の引数にわたって、最終的に X の引数にわたるみたい
            await foreach (var x in X().WithCancellation(cts.Token))
            {
                Console.Write(x);
            }
            Console.WriteLine();
        }
    
        static async IAsyncEnumerable<int> X([EnumeratorCancellation]CancellationToken ct = default)
        {
            for (int i = 0; i < 10; i++)
            {
                if (ct.IsCancellationRequested) yield break;
    
                await Task.Delay(1);
    
                for (int j = 0; j < 10; j++)
                {
                    yield return i * j;
                }
            }
        }
  2. ufcpp commented on May 7, 2019

    @ufcpp
    OwnerAuthor
  3. ufcpp commented on May 22, 2019

    @ufcpp
    OwnerAuthor

    16.2 Preview 1 で EnumeratorCancellation 2個付けたらエラーになるの確認。

  4. ufcpp commented on May 22, 2019

    @ufcpp
    Author
  5. ufcpp commented on May 2, 2022

    @ufcpp
    OwnerAuthor

    もう… いいかな…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions