简体   繁体   English

一个ActionBlock可以链接到另一个带有更多参数的ActionBlock吗?

[英]Can an ActionBlock link to another ActionBlock with more parameters in it?

I am still getting a hang of TPL DataFlow so bear with me. 我仍然对TPL DataFlow感到困惑,所以请多多包涵。

My application requires that queue is executed upon in parallel while retaining its order. 我的应用程序要求在保持其顺序的同时并行执行队列。 This led me to the DataFlow library and what I am trying to do. 这导致我进入了DataFlow库以及我正在尝试做的事情。 I am wondering if there is a way to link one ActionBlock to another and the second taking a value from the first to operate on. 我想知道是否有一种方法可以将一个ActionBlock链接到另一个ActionBlock,而第二个可以从第一个中获取值进行操作。
Pseudo-example: 伪例如:

var block1 = new ActionBlock<ByteBuffer>(buffer => {
   // code generating a hash of the byte buffer to pass to next block 
    ulong hash = generateHash(buffer);
   // this is what i would like to pass to the next ActionBlock
    var tup = Tuple<ByteBuffer, ulong>(buffer, along);
}, dataFlowOpts);
var block2 = new ActionBlock<Tuple<ByteBuffer, ulong>(tup => {
    /* code to act on the buffer and hash */
}, dataFlowOpts);

block1.LinkTo(block2); // Is there something like this that would use the correct params?

Is what I'm trying to do possible? 我想做的事可能吗? Does this even make sense? 这有道理吗? The reason I have these separated into two ActionBlocks is I would like to reuse block2 in another codepath (hashing the contents of a different ByteBuffer differently.) 我将它们分成两个ActionBlocks的原因是我想在另一个代码路径中重用block2(以不同方式散列不同ByteBuffer的内容。)

Maybe there is a better way? 也许有更好的方法? Really, I am just trying to hash objects as they come in in a concurrent fashion while retaining FIFO order as it's way too slow to hash these synchronously. 确实,我只是想对以并行方式出现的对象进行哈希处理,同时保留FIFO顺序,因为它太慢而无法同步哈希这些对象。

Just use the TransformBlock : 只需使用TransformBlock

var block1 = new TransformBlock<ByteBuffer, Tuple<ByteBuffer, ulong>>(buffer => {
    // code generating a hash of the byte buffer to pass to next block 
    ulong hash = generateHash(buffer);
    // this is what i would like to pass to the next ActionBlock
    return Tuple<ByteBuffer, ulong>(buffer, along);
}, dataFlowOpts);
var block2 = new ActionBlock<Tuple<ByteBuffer, ulong>(tup => {
    /* code to act on the buffer and hash */
}, dataFlowOpts);

block1.LinkTo(block2); // Is there something like this that would use the correct params?

Or, probably better option to use a DTO class with two properties: buffer and hash, as Tuple isn't much readable. 或者,可能更好的选择是使用具有两个属性的DTO类:缓冲区和哈希,因为Tuple不太可读。 Also, consider a new C# feature about named tuples. 另外,考虑有关命名元组的C#新功能。

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

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