Dataflow with splitting work to small jobs and then group again

后端 未结 3 1060
终归单人心
终归单人心 2020-12-31 09:36

I need to do this kind of work:

  1. Get Page object from database
  2. For each page get all images and process them (IO bound, for example, upload to CDN)
3条回答
  •  孤城傲影
    2020-12-31 10:09

    Consider merging "Load images" and "Process images" into one TransformBlock block. That way you have no trouble keeping the images of a single page together.

    In order to achieve your concurrency limit goal, use a SemaphoreSlim:

    SemaphoreSlim processImageDopLimiter = new SemaphoreSlim(8);
    
    //...
    
    var page = ...; //TransformBlock block input
    var images = GetImages(page);
    ImageWithPage[] processedImages =
     images
     .AsParallel()
     .Select(i => {
        processImageDopLimiter.WaitOne();
        var result = ProcessImage(i);
        processImageDopLimiter.ReleaseOne();
        return result;
     })
     .ToList();
    return new { page, processedImages };
    

    This will lead to quite a few threads blocked waiting. You can use an asynchronous version of this processing if you like. This is immaterial to the question.

提交回复
热议问题