Skip to content

Instantly share code, notes, and snippets.

@nathanaw
Created July 27, 2018 02:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanaw/579d51cfae8475f8be19aa25a10fbded to your computer and use it in GitHub Desktop.
Save nathanaw/579d51cfae8475f8be19aa25a10fbded to your computer and use it in GitHub Desktop.
Async Governor - Divide and Conquer
// 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