Created
August 5, 2013 20:00
-
-
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.
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
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