How to wait for the result while controller is making parallel call

微笑、不失礼 提交于 2021-02-05 12:26:02

问题


I am trying to find account details from DB (GetAccountDetailAsync) for an array of accounts and would like to run in parallel to make it faster.

[HttpPost]
public async Task<IHttpActionResult> GetAccountsAsync(IEnumerable<int> accountIds)
{

    var resultAccounts = new List<AccountDetail>();

    var task = Task.Run(() =>
    {
        Parallel.ForEach(accountIds, new ParallelOptions
        {
            MaxDegreeOfParallelism = 5 
        }, async accountId =>
        {
            var response = await GetAccountDetailAsync(accountId).ConfigureAwait(false);
            resultAccounts.AddRange(response);

        });
    });

    task.Wait();

    return Ok(resultAccounts);

}

But instead of getting the result I am getting though I've got task.Wait. Not sure why task.Wait is not being blocked.

"An asynchronous module or handler completed while an asynchronous operation was still pending."


回答1:


Parallel.ForEach doesn't work with async actions, but you could start all tasks and then wait for them all to complete using Task.WhenAll:

[HttpPost]
public async Task<IHttpActionResult> GetAccountsAsync(IEnumerable<int> accountIds)
{
    Task<List<AccountDetail>>[] tasks = accountIds.Select(accountId => GetAccountDetailAsync(accountId)).ToArray();
    List<AccountDetail>[] results = await Task.WhenAll(tasks);
    return Ok(results.SelectMany(x => x).ToList());
}



回答2:


Assuming you have or can easily get a method GetAccountDetail without the async part, this would be the easiest way:

[HttpPost]
public async Task<IHttpActionResult> GetAccountsAsync(IEnumerable<int> accountIds)
{
   var resultList = accountIds.AsParallel()
                              .WithDegreeOfParallelism(5)
                              .Select(GetAccountDetail)
                              .ToList();
   return Ok(resultList);
}


来源:https://stackoverflow.com/questions/57675722/how-to-wait-for-the-result-while-controller-is-making-parallel-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!