带有前提条件的数据流TPL实现管道 [英] Dataflow TPL Implementing pipeline with precondition

查看:84
本文介绍了带有前提条件的数据流TPL实现管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于使用Dataflow TPL库实现管道的问题.

I have a question about implementing pipeline using Dataflow TPL library.

我的情况是我有一个需要同时处理某些任务的软件.处理如下所示:首先我们在全局级别处理相册,然后进入相册并分别处理每张图片.假设应用程序具有处理插槽,并且它们是可配置的(为示例起见,假设插槽= 2).这意味着应用程序可以处理以下任一情况:

My case is that I have a software that needs to process some tasks concurrently. Processing looks like this: first we process album at global level, and then we go inside album and process each picture individually. Let's say that application has got processing slots and they are configurable (for the sake of example assume slots = 2). This means that application can process either:

a)同时发行两张专辑
b)一张专辑+一张来自不同专辑的照片
c)同一张相册中的两张照片
d)同一张两张照片同时用于不同的相册

a) two albums on the same time
b) one album + one photo from different album
c) two photos on the same time for same album
d) two photos on the same time for different albums

目前,我实现的过程是这样的:

Currently I implemented process like this:

var albumTransferBlock = new TransformBlock<Album, Album>(ProcessAlbum,
    new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 2 });

ActionBlock<Album> photoActionBlock = new ActionBlock<Album>(ProcessPhoto);

albumTransferBlock.LinkTo(photoActionBlock);


Album ProcessAlbum(Album a)
{
    return a;
}

void ProcessPhoto(Album album)
{
    foreach (var photo in album)
    {
        // do some processing
    }
}

我的问题是,当我同时处理1张相册时,应用程序将永远不会使用两个插槽来处理照片.它满足除c)

The problem I have is that when I process 1 album at the time, application will never use two slots for processing photos. It meets all requirement except c)

有人可以使用DataFlow TPL帮助我解决此问题吗?

Can anyone help me to solve this issue using DataFlow TPL?

推荐答案

我认为我可以回答自己.我所做的是:

I think I can answer myself. What I did is:

1)我使用方法Process()创建了一个接口IProcessor2)使用接口IProcessor包装了AlbumProcessing和PhotoProcessing3)创建了一个以IProcessor为输入并执行Process方法的ActionBlock.

1) I created an interface IProcessor with method Process() 2) wrapped AlbumProcessing and PhotoProcessing with interface IProcessor 3) Created one ActionBlock that takes IProcessor as Input and executes Process method.

4)在处理相册结束时,我正在将所有照片的处理添加到ActionBlock.

4) At the end of processing Album I am adding processing of all photos to ActionBlock.

这100%满足了我的要求.也许有人有其他解决方案?

This fulfills my requiremens in 100%. Maybe someone has some other solution?

这篇关于带有前提条件的数据流TPL实现管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆