简体   繁体   中英

Why is my TPL Dataflow Actionblock not executing in parallell?

I am trying to execute some web requests in parallell but for some reason it all runs sync one after the other and I cannot figure out why

The code is fairly simple as follows

 ConcurrentBag<string> documentsToImportInBatch = new ConcurrentBag<string>();

            var actionBlocHapiTest = new ActionBlock<Tuple<string, string>>(
                async request =>
            {
                var uriArticleTest = $"https://xxxx.com/omni-product"
                    + $"/api/v1/brands/0/articles/{request.Item1}?"
                    + $"channelIds=1&season={request.Item2}";
                var response = await staticHttpClient.GetAsync(uriArticleTest);
                var jsonString = await response.Content.ReadAsStringAsync();

                Article article = new Article
                {
                    Brand = brand,
                    UpdatedAt = DateTime.Now.ToString("s"),
                    Source = source,
                    ItemLevel = Enums.ItemLevel.Article,
                    DataType = Enums.DataType.DetailedItemInformation,
                    DetailedData = JObject.Parse(jsonString)
                };
                documentsToImportInBatch.Add(
                    Newtonsoft.Json.JsonConvert.SerializeObject(article));

            }, new ExecutionDataflowBlockOptions
            {
                MaxDegreeOfParallelism = 10
            });

            foreach (var itemTuple in articleAndSeasonList)
            {
                actionBlocHapiTest.Post(new Tuple<string, string>(
                    itemTuple.Item1, itemTuple.Item2));
            }
            actionBlocHapiTest.Complete();
            await actionBlocHapiTest.Completion;

I can see from performance monitoring that it only executing 1 request or so per second where I expected it to be a lot more.

Why is it not filling the block and emptying it in parallell with 10 parallell requests?

figured out the issue, before my action block I had a web request paging through creating the list that the action block used it would submit one by one to the action block stead of building the list and then submit all to the action block so in fact the block itself did process everything it had it's just that it only got one entry at the time, once I fixed that and sent the full list to the block it worked as expected and processed in parallel as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM