Limit number of Threads in Task Parallel Library

后端 未结 4 1615
悲&欢浪女
悲&欢浪女 2020-12-30 13:27

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 f

4条回答
  •  礼貌的吻别
    2020-12-30 14:02

    Essentially you're going to want to create an Action or Task for each file to upload, put them in a List, and then process that list, limiting the number that can be processed in parallel.

    My blog post shows how to do this both with Tasks and with Actions, and provides a sample project you can download and run to see both in action.

    With Actions

    If using Actions, you can use the built-in .Net Parallel.Invoke function. Here we limit it to running at most 5 threads in parallel.

    var listOfActions = new List();
    foreach (var file in files)
    {
        var localFile = file;
        // Note that we create the Task here, but do not start it.
        listOfTasks.Add(new Task(() => blobBlock.UploadFromFileAsync(localFile, FileMode.Create)));
    }
    
    var options = new ParallelOptions {MaxDegreeOfParallelism = 5};
    Parallel.Invoke(options, listOfActions.ToArray());
    

    This option doesn't make use of the async nature of UploadFromFileAsync though, so you might want to use the Task example below.

    With Tasks

    With Tasks there is no built-in function. However, you can use the one that I provide on my blog.

        /// 
        /// Starts the given tasks and waits for them to complete. This will run, at most, the specified number of tasks in parallel.
        /// NOTE: If one of the given tasks has already been started, an exception will be thrown.
        /// 
        /// The tasks to run.
        /// The maximum number of tasks to run in parallel.
        /// The cancellation token.
        public static async Task StartAndWaitAllThrottledAsync(IEnumerable tasksToRun, int maxTasksToRunInParallel, CancellationToken cancellationToken = new CancellationToken())
        {
            await StartAndWaitAllThrottledAsync(tasksToRun, maxTasksToRunInParallel, -1, cancellationToken);
        }
    
        /// 
        /// Starts the given tasks and waits for them to complete. This will run the specified number of tasks in parallel.
        /// NOTE: If a timeout is reached before the Task completes, another Task may be started, potentially running more than the specified maximum allowed.
        /// NOTE: If one of the given tasks has already been started, an exception will be thrown.
        /// 
        /// The tasks to run.
        /// The maximum number of tasks to run in parallel.
        /// The maximum milliseconds we should allow the max tasks to run in parallel before allowing another task to start. Specify -1 to wait indefinitely.
        /// The cancellation token.
        public static async Task StartAndWaitAllThrottledAsync(IEnumerable tasksToRun, int maxTasksToRunInParallel, int timeoutInMilliseconds, CancellationToken cancellationToken = new CancellationToken())
        {
            // Convert to a list of tasks so that we don't enumerate over it multiple times needlessly.
            var tasks = tasksToRun.ToList();
    
            using (var throttler = new SemaphoreSlim(maxTasksToRunInParallel))
            {
                var postTaskTasks = new List();
    
                // Have each task notify the throttler when it completes so that it decrements the number of tasks currently running.
                tasks.ForEach(t => postTaskTasks.Add(t.ContinueWith(tsk => throttler.Release())));
    
                // Start running each task.
                foreach (var task in tasks)
                {
                    // Increment the number of tasks currently running and wait if too many are running.
                    await throttler.WaitAsync(timeoutInMilliseconds, cancellationToken);
    
                    cancellationToken.ThrowIfCancellationRequested();
                    task.Start();
                }
    
                // Wait for all of the provided tasks to complete.
                // We wait on the list of "post" tasks instead of the original tasks, otherwise there is a potential race condition where the throttler's using block is exited before some Tasks have had their "post" action completed, which references the throttler, resulting in an exception due to accessing a disposed object.
                await Task.WhenAll(postTaskTasks.ToArray());
            }
        }
    

    And then creating your list of Tasks and calling the function to have them run, with say a maximum of 5 simultaneous at a time, you could do this:

    var listOfTasks = new List();
    foreach (var file in files)
    {
        var localFile = file;
        // Note that we create the Task here, but do not start it.
        listOfTasks.Add(new Task(async () => await blobBlock.UploadFromFileAsync(localFile, FileMode.Create)));
    }
    await Tasks.StartAndWaitAllThrottledAsync(listOfTasks, 5);
    

提交回复
热议问题