任务并行库中的线程数限制 [英] Limit number of Threads in Task Parallel Library
问题描述
我有几百个文件需要上传到Azure Blob存储。
我想使用并行任务库。
但是不必运行所有100个线程来上传在文件列表中的foreach中,我如何限制它可以使用的最大线程数并并行完成作业。
还是自动平衡?
I have few hundreds of files i need to upload to Azure Blob Storage.
I want to use parallel task library.
But instead of running all the 100 threads to upload in a foreach on list of files, how can i put a limit on max number of threads that it can use and finish the job in parallel.
or does it balance the things automatically?
推荐答案
您根本不应该为此使用线程。有一个基于 Task
的API,它自然是异步的: CloudBlockBlob.UploadFromFileAsync 。将其与 async / await
和 SemaphoreSlim
配合使用可限制并行上传的数量。
You should not be using threads for this at all. There's a Task
-based API for this, which is naturally asynchronous: CloudBlockBlob.UploadFromFileAsync. Use it with async/await
and SemaphoreSlim
to throttle the number of parallel uploads.
示例(未经测试):
const MAX_PARALLEL_UPLOADS = 5;
async Task UploadFiles()
{
var files = new List<string>();
// ... add files to the list
// init the blob block and
// upload files asynchronously
using (var blobBlock = new CloudBlockBlob(url, credentials))
using (var semaphore = new SemaphoreSlim(MAX_PARALLEL_UPLOADS))
{
var tasks = files.Select(async(filename) =>
{
await semaphore.WaitAsync();
try
{
await blobBlock.UploadFromFileAsync(filename, FileMode.Create);
}
finally
{
semaphore.Release();
}
}).ToArray();
await Task.WhenAll(tasks);
}
}
这篇关于任务并行库中的线程数限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!