BufferBlock和ActionBlock与BoundedCapacity不使用最大DOP

编程入门 行业动态 更新时间:2024-10-24 10:16:46
本文介绍了BufferBlock和ActionBlock与BoundedCapacity不使用最大DOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个code:

var data = new BufferBlock<int>(new DataflowBlockOptions { BoundedCapacity = 1 }); var action = new ActionBlock<int>(async id => { Console.WriteLine("[{0:T}] #{1}: Start", DateTime.Now, id); await Task.Delay(1000); Console.WriteLine("[{0:T}] #{1}: End", DateTime.Now, id); }, new ExecutionDataflowBlockOptions { BoundedCapacity = 1, MaxDegreeOfParallelism = -1 }); data.LinkTo(action, new DataflowLinkOptions { PropagateCompletion = true }); for (var id = 1; id <= 3; id++) { Console.WriteLine("[{0:T}] Sending {1}", DateTime.Now, id); data.SendAsync(id).Wait(); Console.WriteLine("[{0:T}] Sending {1} complete", DateTime.Now, id); } data.Complete(); Task.WhenAll(data.Completion, action.Completion).Wait();

这code让我这样的输出:

And this code gets me this output:

[22:31:22] Sending 1 [22:31:22] Sending 1 complete [22:31:22] Sending 2 [22:31:22] #1: Start [22:31:22] Sending 2 complete [22:31:22] Sending 3 [22:31:23] #1: End [22:31:23] #2: Start [22:31:23] Sending 3 complete [22:31:24] #2: End [22:31:24] #3: Start [22:31:25] #3: End

为什么 ActionBlock 不并行工作的,即使它有一个无限DOP?

Why ActionBlock isn't working in parallel even though it has an unbounded DOP?

推荐答案

的原因,你的 ActionBlock 似乎有一个有限的并行度,是因为它有一个 BoundedCapacity 1 BoundedCapacity (不像 InputCount ),包括作为项目此刻处理。这可以很容易地证明:

The reason your ActionBlock seems to be have a limited degree of parallelism is because it has a BoundedCapacity of 1. BoundedCapacity (unlike InputCount) includes the item being processed at the moment. This can be easily demonstrated:

var block = new ActionBlock<int>(_ => Task.Delay(-1), new ExecutionDataflowBlockOptions { BoundedCapacity = 1, MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded }); await block.SendAsync(4); // Adds a new item await block.SendAsync(4); // Blocks forever

这意味着,当你设置 MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded 块不能接受一个以上的项目在时间等实际限制你的并行度。

That means that while you set MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded the block can't accept more than a single item at the time and so practically limiting your degree of parallelism.

您可以修复,通过设置一个较大的 BoundedCapacity :

You can fix that by setting a larger BoundedCapacity:

var action = new ActionBlock<int>(async id => { Console.WriteLine("[{0:T}] #{1}: Start", DateTime.Now, id); await Task.Delay(1000); Console.WriteLine("[{0:T}] #{1}: End", DateTime.Now, id); }, new ExecutionDataflowBlockOptions { BoundedCapacity = 10, MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded });

更多推荐

BufferBlock和ActionBlock与BoundedCapacity不使用最大DOP

本文发布于:2023-11-17 03:15:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ActionBlock   BufferBlock   DOP   BoundedCapacity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!