c# - TPL数据流优化

标签 c# .net task-parallel-library tpl-dataflow

我以这种方式设置了 TPL 数据流:

  1. 下载字节数组
  2. 处理数据
  3. 将处理后的数据流式传输到另一个位置

此流程效果很好,但偶尔会在下载文件时遇到备份、连接中断等情况。我想做的是并行下载,但仍确保执行步骤 3,以便接收方获得有效负载按正确的顺序排列。

var broadcaster = new BroadcastBlock<string>(d => d);


var downloader = new TransformBlock<string, byte[]>(async data => {
  // Download and return data       
});


var processor = new TransformBlock<byte[], byte[]> (async data => {
  // Process and return data
});


var uploader = new ActionBlock<byte[]>(async input => {
  // Upload file to another location
});


broadcaster.LinkTo(downloader);
downloader.LinkTo(processor);
processor.LinkTo(uploader);


broadcaster.SendAsync("http://someUrl");
broadcaster.SendAsync("http://someOtherUrl")

因此,在上面的代码片段中,我希望两个网址同时下载,但重要的是上传程序在第二个网址之前处理第一个网址。有人能指出我正确的方向吗?

最佳答案

I'd want the two urls to download simultaneously, but it's important that the first one gets processed by the uploader before the second url

然后只需在该 block 上设置MaxDegreeOfParallelism,它的行为就会像这样。当同时下载 URL 1 和 2 并且 2 在 1 之前完成时,它仍然会等待 1 完成,然后再将 2 发送到下一个 block 。

这可能不是最有效的方法,但它确实确保在管道中的所有 block 之间维持处理顺序。

关于c# - TPL数据流优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23901704/

相关文章:

c# - SQL Server Compact 3.5 版数据库访问被拒绝

c# - XML 文档中的引用运算符

c# - 安装应用程序后单击此处

c# - .NET GC 停止桌面应用程序 - 性能问题

c# - 取消任务时的 OperationCanceledException VS TaskCanceledException

c# - asp.net 简单模式用法

.net - XPATH 查询以查找包含关键字的属性

C# - 从异步方法回调更新变量 BY REF - WebClient 类

c# - 线程类 'Splash-Type' 屏幕的 TPL 等效项

c# - MaxDegreeOfParallelism 不适用于任务列表?