首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将产生TransformBlock的IEnumerable<T>链接到接收T的块

将产生TransformBlock的IEnumerable<T>链接到接收T的块
EN

Stack Overflow用户
提问于 2018-06-04 11:20:17
回答 1查看 444关注 0票数 1

我正在编写一个web图库刮刀,我希望尽可能多地与TPL数据流并行处理文件。

为了抓取,我首先获取图库主页,并解析HTML以获得图像页链接为列表。然后,我转到列表中的每个页面,解析HTML以获得指向图像的链接,然后将其保存到磁盘中。

以下是我的计划大纲:

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-04 12:06:36

您可以使用TransformManyBlock代替TransformBlock

代码语言:javascript
复制
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!
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50679418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档