Skip to content

TPL Dataflow TransformManyBlock support for IAsyncEnumerable #30863

Closed

Description

@onionhammer

Currently TransformManyBlock is expected to output an IEnumerable, it would be useful to also have support for an IAsyncEnumerable output

Additional question; why is TPL dataflow in this repository? Seems like an obvious candidate to split into its own repo

Rationale and Usage

The TransforManyBlock of the System.Threading.Tasks.Dataflow library currently supports two types of transformation functions:

  • Func<TInput, IEnumerable<TOutput>>
  • Func<TInput, Task<IEnumerable<TOutput>>>

.Net Core 3.0 (.Net Standard 2.1) introduced the IAsyncEnumerable<T> interface which exposes an enumerator that has a MoveNextAsync() method that can be awaited. It is generally presented as a replacement of the Task<IEnumerable<T>> type, and has been adopted in a lot of async-related APIs.

In the TransforManyBlock context, it could enable some transform functions where we can make asynchronous calls in between yielding results.

For example, if someone uses a sync method inside the transformation function :

var tb = new TransformManyBlock<T,U>(input =>
{
    var iterator = GetItemQueryIterator<U>(input);
    while (iterator.HasMoreResults)
    {
        foreach(var output in iterator.ReadNext())
        {
            yield return output;
        }
    }
});

And wants to migrate it to its async version, he has to modify the code by introducing an instance of an implementation class of the IEnumerable<T> interface (here a List) that he has to fill in "manually" :

var tb = new TransformManyBlock<T,U>(async input =>
{
    var iterator = GetItemQueryIterator<U>(input);
    var outputs = new List<U>();
    while (iterator.HasMoreResults)
    {
        foreach(var output in await iterator.ReadNextAsync())
        {
            outputs.Add(output);
        }
    }
    return outputs;
});

While he expects to only have to activate async/await mechanisms of the .Net framework :

var tb = new TransformManyBlock<T,U>(async input =>
{
    var iterator = GetItemQueryIterator<U>(input);
    while (iterator.HasMoreResults)
    {
        foreach(var output in await iterator.ReadNextAsync())
        {
            yield return output;
        }
    }
});

Proposed API

Such a quick and easy migration can be made possible by adding two new constructors for the TransforManyBlock class.

using System.Collections.Generic;

namespace System.Threading.Tasks.Dataflow
{
    public sealed partial class TransformManyBlock<TInput, TOutput>
    {
        public TransformManyBlock(Func<TInput, IAsyncEnumerable<TOutput>> transform) { }
        public TransformManyBlock(Func<TInput, IAsyncEnumerable<TOutput>> transform, ExecutionDataflowBlockOptions dataflowBlockOptions) { }
    }
}

@stephentoub mentioned: though as long as we're adding support for IAsyncEnumerable<T> to the library, we should probably also add a ReadAllAsync extension method, as we did for System.Threading.Channels.

public static class DataflowBlock
{
    public static IAsyncEnumerable<TOutput> ReceiveAllAsync<TOutput>(this ISourceBlock<TOutput> source, CancellationToken cancellationToken = default);
}

Activity

  1. tarekgh commented on Sep 15, 2019

    @tarekgh
    Member

    @onionhammer could you create the API proposal and add it to the issue description? https://github.com/dotnet/corefx/blob/master/Documentation/project-docs/api-review-process.md

    Additional question; why is TPL dataflow in this repository? Seems like an obvious candidate to split into its own repo

    Why want to split it to its own repo? this seams strange to me. We are not trying to split into more repos and instead we are trying to reduce the repos we have.

  2. manandre commented on Oct 25, 2019

    @manandre
    Contributor

    Very good idea! I am willing to help on that topic.

    Rationale and Usage

    The TransforManyBlock of the System.Threading.Tasks.Dataflow library currently supports two types of transformation functions:

    • Func<TInput, IEnumerable<TOutput>>
    • Func<TInput, Task<IEnumerable<TOutput>>>

    .Net Core 3.0 (.Net Standard 2.1) introduced the IAsyncEnumerable<T> interface which exposes an enumerator that has a MoveNextAsync() method that can be awaited. It is generally presented as a replacement of the Task<IEnumerable<T>> type, and has been adopted in a lot of async-related APIs.

    In the TransforManyBlock context, it could enable some transform functions where we can make asynchronous calls in between yielding results.

    For example, if someone uses a sync method inside the transformation function :

    var tb = new TransformManyBlock<T,U>(input =>
    {
        var iterator = GetItemQueryIterator<U>(input);
        while (iterator.HasMoreResults)
        {
            foreach(var output in iterator.ReadNext())
            {
                yield return output;
            }
        }
    });

    And wants to migrate it to its async version, he has to modify the code by introducing an instance of an implementation class of the IEnumerable<T> interface (here a List) that he has to fill in "manually" :

    var tb = new TransformManyBlock<T,U>(async input =>
    {
        var iterator = GetItemQueryIterator<U>(input);
        var outputs = new List<U>();
        while (iterator.HasMoreResults)
        {
            foreach(var output in await iterator.ReadNextAsync())
            {
                outputs.Add(output);
            }
        }
        return outputs;
    });

    While he expects to only have to activate async/await mechanisms of the .Net framework :

    var tb = new TransformManyBlock<T,U>(async input =>
    {
        var iterator = GetItemQueryIterator<U>(input);
        while (iterator.HasMoreResults)
        {
            foreach(var output in await iterator.ReadNextAsync())
            {
                yield return output;
            }
        }
    });

    Proposed API

    Such a quick and easy migration can be made possible by adding two new constructors for the TransforManyBlock class.

    using System.Collections.Generic;
    
    namespace System.Threading.Tasks.Dataflow
    {
        public sealed partial class TransformManyBlock<TInput, TOutput>
        {
            public TransformManyBlock(Func<TInput, IAsyncEnumerable<TOutput>> transform) { }
            public TransformManyBlock(Func<TInput, IAsyncEnumerable<TOutput>> transform, ExecutionDataflowBlockOptions dataflowBlockOptions) { }
        }
    }
  3. tarekgh commented on Oct 25, 2019

    @tarekgh
    Member

    @stephentoub what you think about this proposal?

  4. stephentoub commented on Oct 25, 2019

    @stephentoub
    Member

    what you think about this proposal?

    I'm fine with it.

    Though as long as we're adding support for IAsyncEnumerable<T> to the library, we should probably also add a ReadAllAsync extension method, as we did for System.Threading.Channels.

    public static class DataflowBlock
    {
        public static IAsyncEnumerable<TOutput> ReceiveAllAsync<TOutput>(this ISourceBlock<TOutput> source, CancellationToken cancellationToken = default);
    }
  5. manandre commented on Oct 25, 2019

    @manandre
    Contributor

    @stephentoub I would propose, in order to be consistent with the current library conventions, to name it ReceiveAllAsync and to split it in two methods (with or without CancellationToken) :

    public static class DataflowBlock
    {
        public static IAsyncEnumerable<TOutput> ReceiveAllAsync<TOutput>(this ISourceBlock<TOutput> source);
        public static IAsyncEnumerable<TOutput> ReceiveAllAsync<TOutput>(this ISourceBlock<TOutput> source, CancellationToken cancellationToken);
    }

    Do you agree ?

  6. tarekgh commented on Oct 25, 2019

    @tarekgh
    Member

    I have moved the proposal up and added what @stephentoub suggested too.

  7. stephentoub commented on Oct 25, 2019

    @stephentoub
    Member

    to name it ReceiveAllAsync

    Yes, that was a copy/paste error. Fixed.

    and to split it in two methods (with or without CancellationToken)

    Not sure. We've started using default for cancellation tokens like this, even in libraries that didn't previously.

  8. transferred this issue fromdotnet/corefxon Feb 1, 2020
  9. added this to the 5.0 milestone on Feb 1, 2020
  10. added
    untriaged
    on Feb 23, 2020
  11. removed
    untriaged
    on Feb 25, 2020
  12. modified the milestones: 5.0, Future on Feb 25, 2020
  13. added
    api-approved
    and removed on Jun 12, 2020
  14. danielleiszen commented on Nov 27, 2020

    @danielleiszen

    Hi, can we expect it being released anytime soon? Or is it already included in a preview package? This would be a huge help in my current use case. Thank you!

  15. tarekgh commented on Nov 27, 2020

    @tarekgh
    Member

    @danielleiszen this feature is approved but it is not implemented yet. I expect this will be implemented at some point and be in one of the .NET 6.0 previews next year. If you are interested to submit a PR for the implementation, you can do so too.

  16. modified the milestones: Future, 6.0.0 on Nov 27, 2020
  17. manandre commented on Nov 27, 2020

    @manandre
    Contributor

    A pull request #37876 is already under review. @danielleiszen your help is welcome.

  18. manandre commented on Mar 6, 2021

    @manandre
    Contributor

    The pull request #37876 has just been merged but it has been narrowed down to the DataflowBlock.ReceiveAllAsync extension method. I will open a new pull request for the initial request concerning the new TransformManyBlock constructors.

  19. eerhardt commented on Aug 12, 2021

    @eerhardt
    Member

    @stephentoub - do you think this API is necessary for 6.0? Or can this issue be marked for 7.0?

  20. modified the milestones: 6.0.0, 7.0.0 on Aug 17, 2021
  21. stephentoub commented on Aug 17, 2021

    @stephentoub
    Member

    It can be 7.0. I was hoping to get it in for 6.0 and really appreciate @manandre's efforts here. This area of the code is just already so complicated, I keep hoping we can find a way to add in this remaining piece without a lot of additional duplication. Hopefully we can get it in soon. In the meantime, though, half of this was completed for 6.0 (the consumption part).

  22. WhatzGames commented on Jun 3, 2022

    @WhatzGames

    Just out of curiosity, is there any progress or did it come to a halt?

  23. stephentoub commented on Jun 3, 2022

    @stephentoub
    Member

    I want to get it in for 7. I'm still exploring if there are better ways that don't have so much duplication.

  24. ghost locked as resolved and limited conversation to collaborators on Aug 11, 2022
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

    api-approvedarea-System.Threading.Tasks

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions