我在 azure blob 存储中有一个 zip 文件,其中包含 2 种类型的文件,A 和 B。
我需要从 azure blob 存储中读取文件并并行处理每个文件。
我在这里尝试最有效的方式读取和处理文件。
对于下面的代码,我随机收到几个文件的错误(比如在 Zip 中我有 10 个文件,3 个成功处理,7 个失败并出现错误
A local file header is corrupt
)
当我尝试在多个
IEnumerable<ZipArchiveEntry>
上发送Task.Run
时,请提出问题是什么?我怎样才能确保两种文件类型的进程并行进行?
private static async Task DownloadFromStream(BlobClient blobClient)
{
using var archive = new ZipArchive(await blobClient.OpenReadAsync(new BlobOpenReadOptions(false)));
//get different file types
var typeAFiles = GetFiles(archive, "TYPE_A");
var typeBFiles= GetFiles(archive, "TYPE_B");
//trying to process each type file in parallel
var fileProcessTasks = new List<Task>
{
Task.Run(async () => await ProcessData(typeAFiles)),
Task.Run(async () => await ProcessData(typeBFiles))
};
await Task.WhenAll(fileProcessTasks);
}
private static async Task ProcessData(IEnumerable<ZipArchiveEntry> files)
{
foreach (var entry in files)
{
try
{
var content = await new System.IO.StreamReader(entry.Open(), Encoding.UTF8).ReadToEndAsync();
Console.WriteLine(content.Length);
Console.WriteLine("*************");
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
private static IEnumerable<ZipArchiveEntry> GetFiles(ZipArchive archive, string fileType)
{
return archive.Entries.Where(y => y.Name.StartsWith(fileType));
}