TPL数据流:设计并行,同时维持秩序 [英] TPL Dataflow: design for parallelism while keeping order

查看:265
本文介绍了TPL数据流:设计并行,同时维持秩序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有与第三方物流合作过,所以我不知道这是否可以用它来完成:
我的应用程序创建从大量帧的GIF图像动画文件。我开始与位图的一个列表,其中重新presents的gif文件的帧,并需要针对每个帧执行以下操作:

I have never worked with TPL before so I was wondering whether this can be done with it: My application creates a gif image animation file from a lot of frames. I start with a list of Bitmap which represents the frames of the gif file and need to do the following for each frame:


  1. 画了一些文本/位图到帧

  2. 作物帧

  3. 调整框架

  4. 将图像缩小为256色

显然,这过程可以并行完成对列表中的所有的帧,但对各帧的步骤的顺序需要是相同的。
在那之后,我需要编写所有的帧的gif文件。因此所有的帧需要在它们在原始列表相同的顺序被接收。最重要的是,这个过程可以开始时的第一帧是准备好了,就没有必要等待,直到所有的帧进行处理。

Obviously this process can be done in parallel for all the frames in the list but for each frame the order of steps needs to be the same. After that, I need to write all the frames to the gif file. Therefore all the frames need to be received in the same order they were in in the original list. On top of that, this process can start when the first frame is ready for it, there is no need to wait until all frames are processed.

这就是这种情况。是TPL数据流适合呢?如果是的话,任何人都可以给我一个提示就如何设计TPL块结构,以反映上述过程说明了正确的方向?这相比于一些样品,我发现似乎很复杂,我。

So that's the situation. Is TPL Dataflow suitable for this? If yes, can anyone give me a hint in the right direction on how to design the tpl block structure to reflect the process explained above? It seems quite complex to me compared to some samples I've found.

推荐答案

我觉得有意义的使用TPL数据流对于这一点,特别是因为它会自动保持在正确的顺序处理的元素,甚至与并行打开。

I think it makes sense to use TPL Dataflow for this, especially since it automatically keeps the processed elements in the right order, even with parallelism turned on.

您可以创建在过程中每一步一个独立的模块,但我觉得没有必要,在这里,用于处理帧的一个块和一个写他们的将是不够的:

You could create a separate block for each step in the process, but I think there is no need for that here, one block for processing the frames and one for writing them will be enough:

public Task CreateAnimationFileAsync(IEnumerable<Bitmap> frames)
{
    var frameProcessor = new TransformBlock<Bitmap, Bitmap>(
        frame => ProcessFrame(frame),
        new ExecutionDataflowBlockOptions
        { MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded });

    var animationWriter = new ActionBlock<Bitmap>(frame => WriteFrame(frame));

    frameProcessor.LinkTo(
        animationWriter,
        new DataflowLinkOptions { PropagateCompletion = true });

    foreach (var frame in frames)
    {
        frameProcessor.Post(frame);
    }

    frameProcessor.Complete();

    return animationWriter.Completion;
}

private Bitmap ProcessFrame(Bitmap frame)
{
    …
}

private async Task WriteFrame(Bitmap frame)
{
    …
}

这篇关于TPL数据流:设计并行,同时维持秩序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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