gpt4 book ai didi

c# - TPL 数据流转换 block 发布到批处理 block ,然后是操作 block

转载 作者:行者123 更新时间:2023-11-30 16:53:09 27 4
gpt4 key购买 nike

我有一个基于 TPL 数据流的应用程序,它仅使用批处理 block 和操作 block 就可以正常工作。

我已经添加了一个 TransformBlock 以尝试在发布到批处理 block 之前从源中转换数据,但我的操作 block 从未被击中。没有错误或异常被抛出。

我不确定我是否需要完成我的变换 block ,因为它似乎只被击中一次。

除了返回输出类型的对象之外,我是否需要在我的转换代码中添加一个步骤?

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";
}
}
}

最佳答案

正如上面“usr”所提到的,我没有传播 block 的完成。以下代码完美运行。

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";
}
}
}

关于c# - TPL 数据流转换 block 发布到批处理 block ,然后是操作 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31804794/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com