[英]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我正在尝试并行执行一些 web 请求,但由于某种原因,它一个接一个地运行同步,我不知道为什么
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.我可以从性能监控中看到它每秒只执行 1 个左右的请求,而我预计它会更多。
Why is it not filling the block and emptying it in parallell with 10 parallell requests?为什么它没有填充块并用 10 个并行请求并行清空它?
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.解决了这个问题,在我的操作块之前,我有一个 web 请求通过创建操作块使用的列表进行分页,它将一一提交给操作块,而不是构建列表,然后将所有内容提交给操作块,所以事实上该块本身确实处理了它所拥有的所有内容,只是它当时只有一个条目,一旦我修复了它并将完整列表发送到它按预期工作并按预期并行处理的块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.