TPL数据流-块未按预期处理

时间:2019-04-24 21:26:31

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

我有一组简单的块,大多数都是以串行方式处理的,但是我有两个要并行处理的块(processblock1和processblock2)。我才刚刚开始使用TPL数据块。 但是,在下面的代码中,我可以看到paraellelblock1被称为,但从未像预期的那样被parallelblock2调用。我希望他们会同时开始。

    class Program
    {
        static void Main(string[] args)
        {
            var readBlock = new TransformBlock<int, int>(x => DoSomething(x, "readBlock"),
                new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 1 }); //1

            var processBlock1 =
                new TransformBlock<int, int>(x => DoSomething(x, "processBlock1")); //2
            var processBlock2 =
                new TransformBlock<int, int>(x => DoSomething(x, "processBlock2")); //3

            var saveBlock =
                new ActionBlock<int>(
                x => Save(x)); //4

            readBlock.LinkTo(processBlock1,
                new DataflowLinkOptions { PropagateCompletion = true }); //5

            readBlock.LinkTo(processBlock2,
                new DataflowLinkOptions { PropagateCompletion = true }); //6


            processBlock1.LinkTo(
                saveBlock); //7

            processBlock2.LinkTo(
                saveBlock); //8

            readBlock.Post(1); //10

            Task.WhenAll(
                processBlock1.Completion,
                processBlock2.Completion)
                .ContinueWith(_ => saveBlock.Complete()); //11

            readBlock.Complete(); //12
            saveBlock.Completion.Wait(); //13
            Console.WriteLine("Processing complete!");
            Console.ReadLine();
        }
        private static int DoSomething(int i, string method)
        {
            Console.WriteLine($"Do Something, callng method : { method}");
            return i;
        }
        private static async Task<int> DoSomethingAsync(int i, string method)
        {
            DoSomething(i, method);
            return i;
        }
        private static void Save(int i)
        {
            Console.WriteLine("Save!");
        }


    }

2 个答案:

答案 0 :(得分:1)

默认情况下,tpl块仅将消息发送到第一个链接的块。 使用BroadcastBlock将消息发送到许多组件。

void Main()
{
    var random = new Random();
    var readBlock = new TransformBlock<int, int>(x => {  return DoSomething(x, "readBlock"); },
            new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 1 }); //1

    var braodcastBlock = new BroadcastBlock<int>(i => i); // ⬅️ Here

    var processBlock1 =
        new TransformBlock<int, int>(x => DoSomething(x, "processBlock1")); //2
    var processBlock2 =
        new TransformBlock<int, int>(x => DoSomething(x, "processBlock2")); //3

    var saveBlock =
        new ActionBlock<int>(
        x => Save(x)); //4

    readBlock.LinkTo(braodcastBlock, new DataflowLinkOptions { PropagateCompletion = true });

    braodcastBlock.LinkTo(processBlock1,
        new DataflowLinkOptions { PropagateCompletion = true }); //5

    braodcastBlock.LinkTo(processBlock2,
        new DataflowLinkOptions { PropagateCompletion = true }); //6


    processBlock1.LinkTo(
        saveBlock); //7

    processBlock2.LinkTo(
        saveBlock); //8

    readBlock.Post(1); //10
    readBlock.Post(2); //10

    Task.WhenAll(
                processBlock1.Completion,
                processBlock2.Completion)
                .ContinueWith(_ => saveBlock.Complete());

    readBlock.Complete(); //12
    saveBlock.Completion.Wait(); //13
    Console.WriteLine("Processing complete!");
}

// Define other methods and classes here

private static int DoSomething(int i, string method)
{
    Console.WriteLine($"Do Something, callng method : { method} {i}");
    return i;
}
private static Task<int> DoSomethingAsync(int i, string method)
{
    DoSomething(i, method);
    return Task.FromResult(i);
}
private static void Save(int i)
{
    Console.WriteLine("Save! " + i);
}

答案 1 :(得分:0)

您似乎只在图表上发布了一个商品,而第一个消费该商品的消费者将获胜。您制作的图中没有隐含的“ tee”功能-因此那里没有并行性。