Processing Multiple Messages using Execution Block
-
Monday, September 24, 2012 12:48 PM
Hi,
I noticed the various execution blocks can processing multiple messages simultaneously. In doing so, however, it still ensures that output messages are propagated in the same order that they arrived at the block. Does that mean the messages behind will need to wait for the messages in front to finish before any output can be generated? If so, is it possible to configure the block to allow message output without this restriction? The messages in front could be long running and could hog the entire flow.
Thanks,
Chew
All Replies
-
Monday, September 24, 2012 11:47 PMOwner
Hi Chew-
I assume you're referring to TransformBlock and TransformManyBlock?
You're correct that such a block set to have a MaxDegreeOfParallelism > 1 can process multiple messages concurrently. And as long as you haven't mucked with BoundedCapacity, it'll continue to do so regardless of when and how messages are propagated to linked targets. The behavior you describe is that it'll only propagate to linked targets in the order the messages were received, so it'll continue to buffer the output messages until it's able to do so. For example, if you send a TransformBlock with DOP==2 messages 1 through 100, and message 3 takes a long time to process, it'll continue to process messages 4, 5, 6, etc. until message 3 completes, but it won't propagate the results of 4, 5, 6, etc. to linked targets until 3 is completed, such that the result of processing 3 can be propagated, and then 4, 5, 6, etc.
There is currently no way to configure that to say "ignore ordering". However, you can always approximate it on your own. For example, instead of using a TransformBlock, you can use an ActionBlock and a BufferBlock, where the ActionBlock just posts to the BufferBlock.
- Marked As Answer by Stephen Toub - MSFTMicrosoft Employee, Owner Tuesday, September 25, 2012 5:33 PM
-
Tuesday, September 25, 2012 4:32 AM
Hi Stephen,
Is it possible to create a custom block out of ActionBlock and BufferBlock that mimics the behaviour I described? I'm kind of new to TPL dataflow. Just came across dataflow a week ago. Appreciate you could show me how it can be done.
I'm actually searching for a framework that can be applied to my project. First I thought of WWF but WWF does not allow piplining. Then I came across TPL dataflow, which looks like the answer to my problem. Do you think dataflow can do whatever in WWF? Basically, I need to integrate several modules that were developed by other developers. The output of one module will feed the input of the other module to form a chain. In between modules, there may be decision point and the output can be routed back or branch out. So the flow graph could be a complex network rather than a sequence flow. Would TPL dataflow be suitable for this scenario?
Thanks,
Chew
-
Tuesday, September 25, 2012 8:42 AM
The basic code to create such block would be:
static IPropagatorBlock<TInput, TOutput> CreateUnorderedTransformBlock<TInput, TOutput>( Func<TInput, TOutput> transform, ExecutionDataflowBlockOptions options) { var source = new BufferBlock<TOutput>(); var target = new ActionBlock<TInput>(x => source.Post(transform(x)), options); return DataflowBlock.Encapsulate(target, source); }You might want to extend it for example to propagate completion or to support asynchronous transforms.
The way you describe your problem, it sounds to me that TPL Dataflow would be a good fit, it works well even with complex dataflow graphs.
- Edited by svick Tuesday, September 25, 2012 8:44 AM
- Marked As Answer by Stephen Toub - MSFTMicrosoft Employee, Owner Tuesday, September 25, 2012 5:33 PM
-
Tuesday, September 25, 2012 2:59 PM
Hi svick,
Thanks for reply and the code snippet. Do you know where can I get more info on building custom block?
Chew
-
Tuesday, September 25, 2012 4:50 PM
A good start is Guide to Implementing Custom TPL Dataflow Blocks by Zlatko Michailov.
- Marked As Answer by Stephen Toub - MSFTMicrosoft Employee, Owner Tuesday, September 25, 2012 5:33 PM
- Unmarked As Answer by chewy1 Tuesday, September 25, 2012 9:40 PM
-
Tuesday, September 25, 2012 9:41 PM
great. thanks.- Marked As Answer by chewy1 Tuesday, September 25, 2012 9:41 PM

