TPL DataFlow与BlockingCollection [英] TPL DataFlow vs BlockingCollection

查看:104
本文介绍了TPL DataFlow与BlockingCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 BlockingCollection 最适合消费者/生产者模式.但是,何时应使用 TPL DataFlow中的 ActionBlock 库?

I understand that a BlockingCollection is best suited for a consumer/producer pattern. However, when do I use a ActionBlock from the TPL DataFlow library?

我最初的理解是关于IO操作,请保留 BlockingCollection ,而CPU密集型操作最适合 ActionBlock .但是我觉得这还不是故事的全部……还有其他见解吗?

My initial understanding is for IO operations, keep the BlockingCollection while CPU intensive operations are bested suited for an ActionBlock. But I feel like this isn't the whole story... Any additional insight?

推荐答案

TPL Dataflow更适合基于角色的设计.这意味着,如果要链接生产者和消费者,使用TDF会容易得多.

TPL Dataflow is better suited for an actor based design. That means that if you want to chain producers and consumers it's much easier with TDF.

TPL数据流的另一个大优点是,它是在考虑 async 的基础上构建的.您可以同时 async 方式(同时同时)生产和消费,这非常有用.(我主要以同步方式进行生产,并以无阻塞的 async 方式进行消耗).

Another big plus for TPL dataflow is that it was built with async in mind. You can both produce and consume in a synchronous way and in an async way (and both at the same time), which is very useful. (I mostly produce in a synchronous way and consume in a non-blocking async way).

您还可以非常轻松地设置有限容量和并行度.

You can also very easily set a bounded capacity and degree of parallelism.

TL; DR: BlockingCollection 是一个简单而通用的工具. TPL数据流更为健壮,但对于某些特定问题来说可能是过大的选择或不合适.

TL;DR: BlockingCollection is a simple and general tool. TPL Dataflow is much more robust, but can be an overkill or a bad fit for specific problems.

这篇关于TPL DataFlow与BlockingCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆