Created
July 27, 2018 02:27
-
-
Save nathanaw/579d51cfae8475f8be19aa25a10fbded to your computer and use it in GitHub Desktop.
Async Governor - Divide and Conquer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using System.Threading.Tasks | |
var maxIterations = 10000; | |
var maxDOP = 10; | |
// Divide into groups. | |
var parallelGroups = Enumerable.Range(0, maxIterations) | |
.GroupBy(r => (r % maxDOP)); | |
var parallelTasks = parallelGroups.Select(groups => | |
{ | |
return Task.Run(async () => | |
{ | |
foreach (var i in groups) | |
{ | |
// Do Async Stuff. (like IO) | |
// await as you please. | |
} | |
}); | |
}); | |
await Task.WhenAll(parallelTasks); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment