Repository navigation
Description
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
tarekgh commentedon Sep 15, 2019
@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
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.
manandre commentedon Oct 25, 2019
Very good idea! I am willing to help on that topic.
Rationale and Usage
The
TransforManyBlockof theSystem.Threading.Tasks.Dataflowlibrary 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 aMoveNextAsync()method that can be awaited. It is generally presented as a replacement of theTask<IEnumerable<T>>type, and has been adopted in a lot of async-related APIs.In the
TransforManyBlockcontext, 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 :
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 aList) that he has to fill in "manually" :While he expects to only have to activate async/await mechanisms of the .Net framework :
Proposed API
Such a quick and easy migration can be made possible by adding two new constructors for the
TransforManyBlockclass.tarekgh commentedon Oct 25, 2019
@stephentoub what you think about this proposal?
stephentoub commentedon Oct 25, 2019
I'm fine with it.
Though as long as we're adding support for
IAsyncEnumerable<T>to the library, we should probably also add aReadAllAsyncextension method, as we did forSystem.Threading.Channels.manandre commentedon Oct 25, 2019
@stephentoub I would propose, in order to be consistent with the current library conventions, to name it
ReceiveAllAsyncand to split it in two methods (with or withoutCancellationToken) :Do you agree ?
tarekgh commentedon Oct 25, 2019
I have moved the proposal up and added what @stephentoub suggested too.
stephentoub commentedon Oct 25, 2019
Yes, that was a copy/paste error. Fixed.
Not sure. We've started using default for cancellation tokens like this, even in libraries that didn't previously.
danielleiszen commentedon Nov 27, 2020
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!
tarekgh commentedon Nov 27, 2020
@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.
manandre commentedon Nov 27, 2020
A pull request #37876 is already under review. @danielleiszen your help is welcome.
manandre commentedon Mar 6, 2021
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
TransformManyBlockconstructors.eerhardt commentedon Aug 12, 2021
@stephentoub - do you think this API is necessary for 6.0? Or can this issue be marked for 7.0?
stephentoub commentedon Aug 17, 2021
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).
WhatzGames commentedon Jun 3, 2022
Just out of curiosity, is there any progress or did it come to a halt?
stephentoub commentedon Jun 3, 2022
I want to get it in for 7. I'm still exploring if there are better ways that don't have so much duplication.