使用Task.blob将大约200 300k blob写入blob存储时,WhenAll(List)的写入挂起/花费的时间明显长于按顺序执行的时间。
我正在功能应用中运行该过程。
不起作用
private async Task WriteToBlobAsync(List<DataSeries> allData)
{
int blobCount = 0;
List<Task> blobWriteTasks = new List<Task>();
foreach(DataSeries series in allData)
{
blobCount++;
string seriesInJson = JsonConvert.SerializeObject(series);
blobWriteTasks.Add(_destinationBlobStore.WriteBlobAsync(seriesInJson, series.SaveName));
//await _destinationBlobStore.WriteBlobAsync(seriesInJson, series.SaveName);
if (blobCount % 100 == 0)
{
_flightSummaryDoc.AddLog($"{blobCount} Blobs Complete");
_log.Info($"{blobCount} Blobs Complete");
}
}
await Task.WhenAll(blobWriteTasks.ToArray());
}
工作明显更快(但不应该如此)
private async Task WriteToBlobAsync(List<DataSeries> allData)
{
int blobCount = 0;
List<Task> blobWriteTasks = new List<Task>();
foreach(DataSeries series in allData)
{
blobCount++;
string seriesInJson = JsonConvert.SerializeObject(series);
//blobWriteTasks.Add(_destinationBlobStore.WriteBlobAsync(seriesInJson,series.SaveName));
await _destinationBlobStore.WriteBlobAsync(seriesInJson, series.SaveName);
if(blobCount % 100 == 0)
{
_flightSummaryDoc.AddLog($"{blobCount} Blobs Complete");
_log.Info($"{blobCount} Blobs Complete");
}
}
//await Task.WhenAll(blobWriteTasks.ToArray());
}
答案 0 :(得分:1)
由于无法处理200个并发请求,它正在缓慢运行并失败。
考虑使用SemaphorSlim
来使用其内置的限制机制,并将并发请求限制为一个更合理的数目。
查看此信息:How to limit the amount of concurrent async I/O operations?