c# - 链接 TransformBlock 产生 IEnumerable阻止接收 T
问题描述
我正在编写一个网络画廊刮板,我想尽可能地使用 TPL Dataflow 并行处理文件。
为了抓取,我首先获取画廊主页并解析 HTML 以获取图像页面链接作为列表。然后我转到列表中的每个页面并解析 HTML 以获取指向图像的链接,然后将其保存到磁盘。
这是我的程序的大纲:
var galleryBlock = new TransformBlock<Uri, IEnumerable<Uri>>(async uri =>
{
// 1. Get the page
// 2. Parse the page to get the urls of each image page
return imagePageLinks;
});
var imageBlock = new TransformBlock<Uri, Uri>(async uri =>
{
// 1. Go to the url and fetch the image page html
// 2. Parse the html to retrieve the image url
return imageUri;
});
var downloadBlock = ActionBlock<Uri>(async uri =>
{
// Download the image from uri to list
});
var opts = new DataflowLinkOptions { PropagateCompletion = true};
galleryBlock.LinkTo(imageBlock, opts); // this doesn't work, as I'm returning a list and not a single Item. However I want to progress that block in parallel.
imageBlock.LinkTo(downloadBlock, opts);
解决方案
您可以使用 aTransformManyBlock
代替您的TransformBlock
:
var galleryBlock = new TransformManyBlock<Uri, Uri>(async uri =>
{
return Enumerable.Empty<Uri>(); //just to get it compiling
});
var imageBlock = new TransformBlock<Uri, Uri>(async uri =>
{
return null; //just to get it compiling
});
var opts = new DataflowLinkOptions { PropagateCompletion = true };
galleryBlock.LinkTo(imageBlock, opts); // bingo!
推荐阅读
- c# - 如何重构此方法以降低其认知复杂性
- mysql - 将 pandas df 写入 mysql 表
- android - 如何创建内部文件或目录以保存从图库中挑选的图像的 uri?
- c# - 通过 Process 类启动时如何使 WPF 窗口表现为 DialogWindow
- c# - EF Core 5 TPT - 具有不同 ID 名称的继承对象
- function - 从 PowerShell 中的不同文件调用函数
- ios - 如何在 Calendar.current.isDateInToday 中考虑时区?
- php - Cypress 测试在本地 Windows XAMPP 上运行的 laravel 应用程序问题
- maven - 将 JS stdlib 链接到生成的 kdoc
- angular - 角度数据表:在 Excel 中导出图像