Daf*_*ffi 9 .net c# task-parallel-library tpl-dataflow
在使用数据流后,我遇到了一个新问题.我想限制所有块的输入队列.我的产品块(ActionBlock)正在快速创建5000个元素并将它们发布到广播块.因此,如果我将广播块的BoundedCapacity设置为100,则会抛出大量数据.但是我希望生产块等待我的缓冲区输入队列中的新槽.
有没有办法摆脱这个问题?
这正是BufferBlock为了什么.如果您设置它BoundedCapacity并且它已满,它将推迟接收任何消息,直到有人消费它们.这意味着例如Post()将阻止SendAsync()并将返回未完成的Task.
EDIT: There is no built-in block that sends to multiple targets and never throws data away. But you can easily build one yourself from ActionBlock and sending loop:
static ITargetBlock<T> CreateMultipleTargetsBlock<T>(
IEnumerable<ITargetBlock<T>> targets, int boundedCapacity)
{
var targetsList = targets.ToList();
var block = new ActionBlock<T>(
async item =>
{
foreach (var target in targetsList)
{
await target.SendAsync(item);
}
},
new ExecutionDataflowBlockOptions { BoundedCapacity = boundedCapacity });
// TODO: propagate completion from block to targets
return block;
}
Run Code Online (Sandbox Code Playgroud)
This code assumes that you don't need to clone the data for each target and that the list of targets never changes. Modifying the code for that should be fairly simple.
| 归档时间: |
|
| 查看次数: |
2314 次 |
| 最近记录: |