gpt4 book ai didi

C# 并行复制 - 小文件问题

转载 作者:太空宇宙 更新时间:2023-11-03 12:13:40 24 4
gpt4 key购买 nike

我有一个 C# Azure 函数,用于从 Blob 读取文件内容并将其写入 Azure Data Lake 目标。该代码对于大尺寸文件(~8 MB 及以上)工作得非常好,但对于小尺寸文件,目标文件写入 0 字节。我尝试将 block 大小更改为较小的数字,并将并行线程更改为 1,但行为保持不变。我正在模拟 Visual Studio 2017 中的代码。

请找到我正在使用的代码片段。我已经阅读了有关 Parallel.ForEach 限制的文档,但没有遇到任何与文件大小问题有关的具体内容。 (https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/potential-pitfalls-in-data-and-task-parallelism)

        int bufferLength = 1 * 1024 * 1024;//1 MB chunk
long blobRemainingLength = blob.Properties.Length;
var outPutStream = new MemoryStream();
Queue<KeyValuePair<long, long>> queues = new
Queue<KeyValuePair<long, long>>();

long offset = 0;
while (blobRemainingLength > 0)
{
long chunkLength = (long)Math.Min(bufferLength, blobRemainingLength);
queues.Enqueue(new KeyValuePair<long, long>(offset, chunkLength));
offset += chunkLength;
blobRemainingLength -= chunkLength;
}
Console.WriteLine("Number of Queues: " + queues.Count);

Parallel.ForEach(queues,
new ParallelOptions()
{
//Gets or sets the maximum number of concurrent tasks
MaxDegreeOfParallelism = 10
}, (queue) =>
{
using (var ms = new MemoryStream())
{
blob.DownloadRangeToStreamAsync(ms, queue.Key,
queue.Value).GetAwaiter().GetResult();
lock (mystream)
{

var bytes = ms.ToArray();
Console.WriteLine("Processing on thread {0}",
Thread.CurrentThread.ManagedThreadId);
mystream.Write(bytes, 0, bytes.Length);

}

}
});

感谢所有的帮助!!

最佳答案

我发现我的代码存在问题。 ADL Stream writer 未正确刷新和处置。添加必要的代码后,小/大文件的并行化工作正常。

感谢您的建议!!

关于C# 并行复制 - 小文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50821028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com