Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
564 views
in Technique[技术] by (71.8m points)

c# - Dataflow TPL Implementing pipeline with precondition

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

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) 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
    }
}

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)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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) At the end of processing Album I am adding processing of all photos to ActionBlock.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...