简体   繁体   English

TPL数据流块将永远运行。 永远运行的生产者,使用者建模和异常处理

[英]TPL Dataflow blocks run forever. Forever running producer, consumer modelling and exception handling

I am using TPL Dataflow library to implement a producer consumer scenario. 我正在使用TPL Dataflow库来实现生产者使用者方案。 The processing involves a pipeline of tasks. 该处理涉及一系列任务。 Dataflow library aptly suits my use case. 数据流库非常适合我的用例。

But I want to know how do I efficiently implement this use case [details below]. 但是我想知道如何有效地实现这个用例[下面有详细信息]。

I want to use TPL Dataflow in a server type setting. 我想在服务器类型设置中使用TPL Dataflow。

By a server type setting I mean production of data stream takes place continuously [asynchronously] forever. 通过服务器类型设置,我的意思是数据流的产生永远(不连续)永远发生。 Consumption task also runs forever and consumes all the data produced by a producer[asynchronusly]. 消耗任务也将永远运行,并消耗生产者[异步地]产生的所有数据。 Thus my blocks run forever 因此,我的积木永远运行

How do I model this scenario efficiently? 如何有效地模拟这种情况? Moreover how can I deal with exception handling as I cannot Wait() [Without a call to Wait() I would not be able to catch the exceptions thrown on a faulted block as far as my understanding goes]. 此外,由于无法Wait() ,我该如何处理异常处理[就我的理解而言,如果没有对Wait()的调用,我将无法捕获抛出在故障块上的异常]。

Exceptions 例外

I usually wrap the delegates with exception handling because as you said a block's exception is stored in the Completion task and moreover a faulted block stays faulted so you would need to replace it to move on. 我通常用异常处理包装委托,因为正如您所说的那样,块的异常存储在Completion任务中,而且有故障的块仍然有故障,因此您需要替换它以继续进行。

var block = new TransfromBlock<string, int>(number =>
{
    try
    {
        return int.Parse(number);
    }
    catch (Exception e)
    {
        Trace.WriteLine(e);
    }
});

Capacity 容量

Another important issue is capping. 另一个重要问题是上限。 If some part of your workflow can't handle the load, it's input queue would simply grow infinitely. 如果您的工作流程中的某些部分无法处理负载,那么输入队列将无限增长。 That could lead to a memory leak or OutOfMemoryException s. 这可能导致内存泄漏或OutOfMemoryException So it's important to make sure to limit all your blocks with the appropriate BoundedCapacity and decide what to do when that limit is reached ("throw" items, save to storage, etc.) 因此,重要的是要确保用适当的BoundedCapacity限制所有块,并确定达到该限制时该怎么做(“抛出”项目,保存到存储区等)。

Parallelism 排比

While the default value for BoundedCapacity is -1 (unbounded), the default value for MaxDegreeOfPrallelism is 1 (no parallelism). 虽然BoundedCapacity的默认值为-1(无界),但MaxDegreeOfPrallelism的默认值为1(无并行性)。 Most applications can easily benefit from parallelism so make sure to set an appropriate MaxDegreeOfPrallelism value. 大多数应用程序可以很容易地从并行性中受益,因此请确保设置适当的MaxDegreeOfPrallelism值。 When a block's delegate is purely CPU-intensive MaxDegreeOfPrallelism shouldn't be much higher than the available cores. 当一个块的委托纯粹是CPU密集型时, MaxDegreeOfPrallelism不应比可用内核高很多。 As it has less CPU and more I/O-intensive parts the MaxDegreeOfPrallelism can be increased. 由于它具有较少的CPU和更多的I / O密集型部件, MaxDegreeOfPrallelism可以提高MaxDegreeOfPrallelism

Conclusion 结论

Using TPL dataflow throughout the application's lifetime is really simple. 在应用程序的整个生命周期中使用TPL数据流非常简单。 Just make sure to enable configuration through the app.config and tweak according to actual results "in the field". 只需确保通过app.config启用配置,然后根据“现场”的实际结果进行调整即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM