I have 300 000 sale orders which needs to send in batches of either 200 or 1000/batch to a RestAPI call and with multi threading and using Semaphore
and limit MaxDegreeOfParallelism = 8
(better be number of CPU cores). Response of each batch orders needs to add into a generic list. Please suggest any possibility to get list of API response for all 300k orders.
Parallel.ForEach(
totalSalesOrdersList.Batch(1000),
new ParallelOptions() { MaxDegreeOfParallelism = 8 /* better be number of CPU cores */ },
batchOfSalesOrders => {
DoMyProcessing(batchOfSalesOrders);
});
public static class LinqExtensions
{
public static IEnumerable<IEnumerable<TSource>> Batch<TSource>(
this IEnumerable<TSource> source, int size)
{
TSource[] bucket = null;
var count = 0;
foreach (var item in source)
{
if (bucket == null)
bucket = new TSource[size];
bucket[count++] = item;
if (count != size)
continue;
yield return bucket;
bucket = null;
count = 0;
}
if (bucket != null && count > 0)
yield return bucket.Take(count);
}
}