TPL Dataflow异步调度

时间:2014-11-27 12:32:22

标签: c# .net task-parallel-library async-await tpl-dataflow

async Task的调度不能像我在TPL Dataflow中预期的那样工作。在下面的示例中,我希望ActionBlockTransformBlock可用时立即处理数据。但它正在等待第二个(延迟)结果,然后才进入第三个结果。我在这里误解了什么?处理顺序是否有一些要求?

public class TestDataFlow
{
    public System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();

    public async Task Flow()
    {
        watch.Start();

        var plus10 = new TransformBlock<int, int>(async input =>
        {
            if (input == 2)
            {
                await Task.Delay(5000);
            }
            Console.WriteLine("Exiting plus10 for input {0} @ {1}", input, watch.Elapsed);
            return input + 10;
        },
        new ExecutionDataflowBlockOptions
        {
            MaxDegreeOfParallelism = 4,
        });

        var printSolution = new ActionBlock<int>(input =>
        {
            Console.WriteLine("Solution: {0} @ {1}", input, watch.Elapsed.TotalMilliSeconds);
        },
        new ExecutionDataflowBlockOptions
        {
            MaxDegreeOfParallelism = 4,
        });

        plus10.LinkTo(printSolution);

        List<int> inputs = new List<int> { 1, 2, 3 };
        foreach (var input in inputs)
        {
            await plus10.SendAsync(input);
        }
    }
}

输出:

Exiting plus10 for input 1 @ 115.8583
Exiting plus10 for input 3 @ 116.6973
Solution: 11 @ 126.0146
Exiting plus10 for input 2 @ 5124.4074
Solution: 12 @ 5124.9014
Solution: 13 @ 5126.4834

2 个答案:

答案 0 :(得分:5)

TPL Dataflow保证输入和输出队列的顺序,无论并行处理多少项。

  

&#34;因为每个预定义的源数据流块类型都保证消息按接收顺序传播出去,所以必须在源块处理下一个消息之前从源块读取每条消息。

来自Dataflow (Task Parallel Library)

如果您希望项目在完成处理时准确转移到下一个区块,则应自行明确转移这些项目,这会将您的TransformBlock变为ActionBlock

var printSolution = new ActionBlock<int>(input =>
{
    Console.WriteLine("Solution: {0} @ {1}", input, watch.Elapsed.TotalMilliSeconds);
},executionDataflowBlockOptions);

var plus10 = new ActionBlock<int>(async input =>
{
    if (input == 2)
    {
        await Task.Delay(5000);
    }
    Console.WriteLine("Exiting plus10 for input {0} @ {1}", input, watch.Elapsed);
    await printSolution.SendAsync(input + 10);
}, executionDataflowBlockOptions);

答案 1 :(得分:2)

截至(至少)System.Threading.Tasks.Dataflow.4.6.0ExecutionDataflowBlockOptions现在有一个属性EnsureOrdered,可以设置为false

更新:

Install-Package System.Threading.Tasks.Dataflow

代码:

var options = new ExecutionDataflowBlockOptions {
  EnsureOrdered = false
};
var transform = new TransformBlock<int, int>(i => Transform(i), options);

更多示例:https://stackoverflow.com/a/38865414/625919

发展历史,我认为它很整洁:https://github.com/dotnet/corefx/issues/536 https://github.com/dotnet/corefx/pull/5191