我以这种方式设置了 TPL 数据流:
- 下载字节数组
- 处理数据
- 将处理后的数据流式传输到另一个位置
此流程效果很好,但偶尔会在下载文件时遇到备份、连接中断等情况。我想做的是并行下载,但仍确保执行步骤 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/