[英]TPL Dataflow Transform block post to batch block followed by actionblock
I have a TPL Dataflow based application, that worked fine using only a batch block, then an action block. 我有一个基于TPL数据流的应用程序,只使用批处理块,然后是一个动作块,工作正常。
I've added in a TransformBlock to try and trasnform the data from the source before posting to the batch block, but my action block is never getting hit. 我已经在TransformBlock中添加了尝试并在发布到批处理块之前从源中转换数据,但是我的动作块永远不会被击中。 There are no errors or exceptions being thrown. 没有抛出错误或异常。
I am unsure if I need to complete my transform block, as it only seems to be being hit once. 我不确定我是否需要完成我的变换块,因为它似乎只被击中一次。
Is there a step that I need to add to my transform code other than returning an object of the output type? 除了返回输出类型的对象之外,是否需要添加到转换代码中的步骤?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
namespace DataflowTest
{
class Program
{
public const int BATCH_SIZE = 10;
static void Main(string[] args)
{
Console.WriteLine("Application started");
//Create the pipeline of actions
var transformBlock = new TransformBlock<string, string>(input => TransformString(input), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 1 });
var batchBlock = new BatchBlock<string>(BATCH_SIZE);
var uploadFilesToAzureBlock = new ActionBlock<IEnumerable<string>>(strings => OutputStrings(strings), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 1 });
Console.WriteLine("Blocks created");
//link the actions
transformBlock.LinkTo(batchBlock);
batchBlock.LinkTo(uploadFilesToAzureBlock);
batchBlock.Completion.ContinueWith(obj => uploadFilesToAzureBlock.Complete());
Console.WriteLine("Blocks linked");
var testInputs = new List<string>
{
"Kyle",
"Stephen",
"Jon",
"Conor",
"Adrian",
"Marty",
"Richard",
"Norbert",
"Kerri",
"Mark",
"Declan",
"Ray",
"Paul",
"Andrew",
"Rachel",
"David",
"Darrell"
};
Console.WriteLine("Data created");
var i = 0;
foreach (var name in testInputs)
{
Console.WriteLine("Posting name {0}", i);
transformBlock.Post(name);
i++;
}
batchBlock.Complete();
uploadFilesToAzureBlock.Completion.Wait();
Console.WriteLine("Finishing");
Console.ReadKey();
}
private static void OutputStrings(IEnumerable<string> strings)
{
Console.WriteLine("Beginning Batch...");
foreach (var s in strings)
{
Console.WriteLine(s);
}
Console.WriteLine("Completing Batch...");
}
private static string TransformString(string input)
{
return input += " has been processed";
}
}
}
As mentioned by "usr" above, I had not propagated the completion of the blocks. 如上面的“usr”所述,我没有传播块的完成。 The following code works perfectly. 以下代码完美无缺。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
namespace DataflowTest
{
class Program
{
public const int BATCH_SIZE = 10;
static void Main(string[] args)
{
Console.WriteLine("Application started");
//Create the pipeline of actions
var transformBlock = new TransformBlock<string, string>(input => TransformString(input), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 2 });
var batchBlock = new BatchBlock<string>(BATCH_SIZE);
var outputStringsBlock = new ActionBlock<IEnumerable<string>>(strings => OutputStrings(strings), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 2 });
Console.WriteLine("Blocks created");
//link the actions
transformBlock.LinkTo(batchBlock, new DataflowLinkOptions { PropagateCompletion = true });
batchBlock.LinkTo(outputStringsBlock, new DataflowLinkOptions { PropagateCompletion = true });
batchBlock.Completion.ContinueWith(obj => outputStringsBlock.Complete());
Console.WriteLine("Blocks linked");
var testInputs = new List<string>
{
"Kyle",
"Stephen",
"Jon",
"Conor",
"Adrian",
"Marty",
"Richard",
"Norbert",
"Kerri",
"Mark",
"Declan",
"Ray",
"Paul",
"Andrew",
"Rachel",
"David",
"Darrell"
};
Console.WriteLine("Data created");
var i = 0;
foreach (var name in testInputs)
{
Console.WriteLine("Posting name {0}", i);
transformBlock.Post(name);
i++;
}
transformBlock.Complete();
outputStringsBlock.Completion.Wait();
Console.WriteLine("Finishing");
Console.ReadKey();
}
private static void OutputStrings(IEnumerable<string> strings)
{
Console.WriteLine("Beginning Batch...");
Console.WriteLine("");
foreach (var s in strings)
{
Console.WriteLine(s);
}
Console.WriteLine("");
Console.WriteLine("Completing Batch...");
}
private static string TransformString(string input)
{
return input += " has been processed";
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.