TPL数据流,怎么着大量链接的目标块中的项目只有一个特定的目标块? [英] TPL Dataflow, how to forward items to only one specific target block among many linked target blocks?

查看:234
本文介绍了TPL数据流,怎么着大量链接的目标块中的项目只有一个特定的目标块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找可持有多单的项目,它可以链接到多个目标块多TPL数据流块解决方案,但其中有一个项目转交只有通过过滤器的特定目标块的能力/谓词。在任何时候都不能的项目的同时被输送到多个目标块,始终只对其中一个相匹配的滤波器或项可以被丢弃。我不喜欢BroadCastBlock的,因为,如果我理解正确的话,它不保证交付(或做它?)和滤波对象块端完成,这意味着BroadCastBlock本质上发送的每个项目的副本到所有目标linkedTo块。同时,如果我理解正确并不在任何时候持有多个项目。我不想使用POST /异步,但保持LinkTo链。

I am looking for a TPL data flow block solution which can hold more than a single item, which can link to multiple target blocks, but which has the ability to forward an item to only a specific target block that passes a filter/predicate. At no time should an item be delivered to multiple target blocks at the same time, always only to the one which matches the filter or the item can be discarded. I am not fond of BroadCastBlock because, if I understand correctly, it does not guarantee delivery (or does it?) and the filtering is done on the target block side, meaning BroadCastBlock essentially sends copies of each item to all linkedTo target blocks. It also does not hold more than one item at any time if I understand correctly. I do not want to use Post/Async but maintain a LinkTo chain.

有没有解决一个完整的自定义数据流块的方法吗?还是我误解BroadCastBlock是如何工作的?不幸的还真是不多文档在那里,进入细节和涵盖的用例。任何想法受到了高度评​​价。

Is there a way around a complete custom data flow block? Or am I misunderstanding how BroadCastBlock works? Unfortunately there really is not much documentation out there that goes into detail and covers use cases. Any ideas are highly appreciated.

推荐答案

如果我理解正确的话,你希望可以通过一个简单的实现什么 BufferBlock ,这将链接到与谓词所有目标块。你还能(无条件),其链接到 。NullTarget ,丢弃不匹配的项目。

If I understand you correctly, what you want could be accomplished by a simple BufferBlock, which would be linked to all your target blocks with predicates. You would also (unconditionally) link it to a NullTarget block, to discard items that didn't match.

是这样的:

var forwarder = new BufferBlock<SomeType>();
forwarder.LinkTo(target1, item => matchesTarget1(item));
forwarder.LinkTo(target2, item => matchesTarget2(item));
forwarder.LinkTo(DataflowBlock.NullTarget<SomeType>());

此方式,每个项目将被发送到匹配,如果有任何的第一个目标。

This way, each item will be sent to the first target that matches, if there is any.

BroadcastBlock ,如果你想给每个项目发送到多个目标可能是有用的,或者如果你想,如果丢弃的物品上述对象块不够快。

BroadcastBlock can be useful if you want to send each item to multiple targets, or if you want to discard items if the target block is not fast enough.

通过 BroadcastBlock ,项目可能如果没有块接受他们(被丢弃,甚至尽管他们可能以后能够接受它)。不过,这并不随机掉落物品,因此,如果你的目标块没有 BoundedCapacity 集,我觉得你可以肯定,他们将得到所有的项目,他们不T下降(由 LinkTo使用谓词() EG)。

With BroadcastBlock, items may be dropped if no block accepts them (even though they may be able to accept it later). But it doesn't drop items at random, so if your target blocks don't have BoundedCapacity set, I think you can be sure that they will get all items that they don't decline (e.g. by using predicate in LinkTo()).

这篇关于TPL数据流,怎么着大量链接的目标块中的项目只有一个特定的目标块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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