问题
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