![](/img/trans.png)
[英]Running parallel async tasks and return result in .NET Core Web API
[英]Find Result of Parallel Async Tasks
基于这个问题,我正在尝试设置代码以将多个图像并行保存到Azure Blob存储。 下面这个方法工作正常,等待Task.WhenAll(任务)等待所有人完成后再继续。
唯一的麻烦是,我希望能够找出每个在我们的数据库中存储信息的请求是否真的成功了。 _db.AddImageAsync返回一个bool,下面的代码等待所有任务完成,但是当我检查所有任务的结果时,每个都是假的(即使我在括号内实际返回true)。
Enumerable中的每个任务都表示,即使我逐步执行断点并且每个任务都已执行,结果仍未计算。
var tasks = wantedSizes.Select(async (wantedSize, index) =>
{
var resize = size.CalculateResize(wantedSize.GetMaxSize());
var quality = wantedSize.GetQuality();
using (var output = ImageProcessHelper.Process(streams[index], resize, quality))
{
var path = await AzureBlobHelper.SaveFileAsync(output, FileType.Image);
var result = await _db.AddImageAsync(id, wantedSize, imageNumber, path);
return result;
}
});
await Task.WhenAll(tasks)
if (!tasks.All(task => task.Result))
return new ApiResponse(ResponseStatus.Fail);
任何帮助深表感谢!
因为.Select(
延迟求值并返回一个IEnumerable<Task<bool>>
你导致.Select(
当你多次遍历结果时多次运行。在它上面抛出一个.ToList()
使它成为一个List<Task<bool>>
并且只执行.Select(
一次,多次枚举将在返回的List<Task<bool>>
,这将没有副作用。
var tasks = wantedSizes.Select(async (wantedSize, index) =>
{
var resize = size.CalculateResize(wantedSize.GetMaxSize());
var quality = wantedSize.GetQuality();
using (var output = ImageProcessHelper.Process(streams[index], resize, quality))
{
var path = await AzureBlobHelper.SaveFileAsync(output, FileType.Image);
//Double check your documentation, is _db.AddImageAsync thread safe?
var result = await _db.AddImageAsync(id, wantedSize, imageNumber, path);
return result;
}
}).ToList(); //We run the Select once here to process the .ToList().
await Task.WhenAll(tasks) //This is the first enumeration of the variable "tasks".
if (!tasks.All(task => task.Result)) //This is a 2nd enumeration of the variable.
return new ApiResponse(ResponseStatus.Fail);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.