填充BatchBlock与调用下一个ActionBlock之间的延迟

时间:2019-03-18 17:14:44

标签: c# .net multithreading task-parallel-library tpl-dataflow

我试图了解BatchBlocksActionBlocks在.net DataFlow中的工作方式

我有一个ActionBlock叫它A1,它是从循环中馈送的,A1会异步地从API中获取一些记录。返回结果后,它会将结果发布回批量大小为5的“ BatchBlock”(B1)中,这将对结果进行批量处理并将其发布到另一个操作(A2)

现在我的期望是A1尽快获得5个结果,应该使用result [4]调用A2。确实会发生这种情况,但不是马上发生,在A1向B1中发布5个项目然后B1调用A2之间要等待很长时间。

不确定为什么要等待这么长时间。

A1--

var i = 0;
action = new ActionBlock<D1>(async d1 =>
           {
               var results = await QueryClientAsync(d1);
               if (results != null)
               {
                   await outgoingQueue.SendAsync(new Tuple<D1, IEnumerable<ScannedRecord>>(d1, GetRecordsFromQuery(d1.Name, results)));
                   Console.WriteLine($"Received {i++} {d1.Name} {DateTime.Now}");
               }
           }, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = MaxDegreeOfParallellismForQuery });

OutgoingQueue在哪里

outgoingQueue = new BatchBlock<Tuple<D1, IEnumerable<ScannedRecord>>>(BatchSizeForOutgoingCollection); // batch size = 5

,outingQueue这样链接到A2-

var actionToProcessIncomingItems = new ActionBlock<Tuple<D1, IEnumerable<ScannedRecord>>[]>(
                ds =>
                {

                    Console.WriteLine($"Batch Recieved at {DateTime.Now}!");
                    ProcessCommonItems(ds);
                }
                , new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = MaxDegreeOfParalleismToProcessRecievedItemsForScanner });

outgoingQueue.LinkTo(actionToProcessIncomingItems, new DataflowLinkOptions { PropagateCompletion = true });

0 个答案:

没有答案