How do I split and merge this dataflow pipeline?

后端 未结 2 742
执念已碎
执念已碎 2020-12-07 03:59

I am trying to create a dataflow using tpl with the following form:

                    -> LoadDataBlock1 -> ProcessDataBlock1 ->  
GetInputPathsBlo         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 04:06

    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 TransformBlocks 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;
    }
    

提交回复
热议问题