我有一个定义了BoundedCapacity的BatchBlock
var _batchBlock = new BatchBlock<int>(2,new GroupingDataflowBlockOptions {BoundedCapacity = 100 });
因此,如果队列容量达到100,则该块会推迟收到的每条消息,直到某个地点可用.
在这种情况下,批处理队列被认为是贪婪还是非贪婪?
解决方法
该块是贪婪的,但不是因为它如何处理100以上的项目,而是因为2以下的项目.您可以将贪婪值设置为false(默认值为true)然后块只会在有足够数量时实际使用项目发送一批,直到那时它们被推迟:
var batchBlock = new BatchBlock<int>(2,new GroupingDataflowBlockOptions { Greedy = false,BoundedCapacity = 100. });
The BatchBlock class operates in either greedy or non-greedy mode. In greedy mode,which is the default,a BatchBlock object accepts every message that it is offered and propagates out an array after it receives the specified count of elements. In non-greedy mode,a BatchBlock object postpones all incoming messages until enough sources have offered messages to the block to form a batch