【问题标题】:Does BoundedCapacity include items currently being processed in TPL Dataflow?BoundedCapacity 是否包括当前在 TPL 数据流中处理的项目?
【发布时间】:2014-10-27 18:43:46
【问题描述】:

BoundedCapacity 限制是否仅包括输入队列中等待处理的项目,还是它也计算当前正在处理的项目?

让我们以ActionBlock为例:

var block = new ActionBlock<int>(
    i => Console.WriteLine(i),
    new ExecutionDataflowBlockOptions
    {
        BoundedCapacity = 1000,
        MaxDegreeOfParallelism = 10,
    });

如果当前有 5 个项目正在并行处理。这是否意味着输入队列可以容纳 1000 个以上在其中,还是只有 995 个?

【问题讨论】:

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


    【解决方案1】:

    显然,BoundedCapacity 确实包括在输入队列中等待的项目之上正在处理的项目。这可以通过ActionBlockExecutionDataflowBlockOptions 与永远不会结束的action 轻松演示:

    var block = new ActionBlock<int>(
        _ => Task.Delay(-1),
        new ExecutionDataflowBlockOptions
        {
            BoundedCapacity = 1000,
            MaxDegreeOfParallelism = 10,
        });
    
    for (int i = 0; i < 1001; i++)
    {
        Console.WriteLine("#{0} - InputCount={1}", i, block.InputCount);
        await block.SendAsync(i);
    }
    

    输出如下,然后应用程序将无限期阻塞:

    ...
    ...
    #990 - InputCount=980
    #991 - InputCount=981
    #992 - InputCount=982
    #993 - InputCount=983
    #994 - InputCount=984
    #995 - InputCount=985
    #996 - InputCount=986
    #997 - InputCount=987
    #998 - InputCount=988
    #999 - InputCount=989
    #1000 - InputCount=990
    

    这是因为添加了 1000 个项目,其中 10 个 (MaxDegreeOfParallelism) 正在同时处理,其他 990 个在输入队列中等待,第 1001 个st 项目永远无法进入。

    【讨论】:

    • 所以 BoundedCapacity 也是最大 DOP 的上限。
    • @usr 没错。 This 是我如何回答这个问题的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 2018-02-16
    相关资源
    最近更新 更多