I am trying to create a dataflow using tpl with the following form:
-> LoadDataBlock1 -> ProcessDataBlock1 ->
GetInputPathsBlo
I would use a nested block to avoid splitting my monthly data and then having to merge them again. Here is an example of two nested TransformBlock
s that process all days of the year 2020:
var monthlyBlock = new TransformBlock>(async (month) =>
{
var dailyBlock = new TransformBlock(async (day) =>
{
await Task.Delay(100); // Simulate async work
return day.ToString();
}, new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = 4 });
foreach (var day in Enumerable.Range(1, DateTime.DaysInMonth(2020, month)))
await dailyBlock.SendAsync(day);
dailyBlock.Complete();
var dailyResults = await dailyBlock.ToListAsync();
return dailyResults;
}, new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = 1 });
foreach (var month in Enumerable.Range(1, 12))
await monthlyBlock.SendAsync(month);
monthlyBlock.Complete();
For collecting the daily results of the inner block I used the extension method ToListAsync
that is shown below:
public static async Task> ToListAsync(this IReceivableSourceBlock block,
CancellationToken cancellationToken = default)
{
var list = new List();
while (await block.OutputAvailableAsync(cancellationToken).ConfigureAwait(false))
{
while (block.TryReceive(out var item))
{
list.Add(item);
}
}
await block.Completion.ConfigureAwait(false); // Propagate possible exception
return list;
}