Skip to content

Instantly share code, notes, and snippets.

@klumsy
Created August 5, 2013 20:00
Show Gist options
  • Save klumsy/6159062 to your computer and use it in GitHub Desktop.
Save klumsy/6159062 to your computer and use it in GitHub Desktop.
my previous batch function wasn't always in parallel. This one seems to work properly and foces the parallelism plus gives you an option on the parallelism settings.
public static IEnumerable<T1> Batch2<T, T1>(
this IEnumerable<T> list,
int batchSize,
int maxParallelism,
Func<IEnumerable<T>, IEnumerable<T1>> action)
{
int i = 0;
return (
from item in list
group item by (int)i++ / batchSize
into part
select part.AsEnumerable())
.AsParallel()
.WithExecutionMode(ParallelExecutionMode.ForceParallelism)
.WithDegreeOfParallelism(maxParallelism).SelectMany(x => action(x));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment