async Task <HashSet <string> > GetPatientSetAsync(IReadOnlyCollection <LeafQuery> queries, CancellationToken token) { var partials = new ConcurrentBag <PartialPatientCountContext>(); var tasks = new List <Task>(); using (var throttler = new SemaphoreSlim(clinDbOpts.Cohort.MaxParallelThreads)) { foreach (var q in queries) { await throttler.WaitAsync(); tasks.Add( Task.Run(async() => { var result = await GetPartialContext(q, token); throttler.Release(); partials.Add(result); }) ); } await Task.WhenAll(tasks); } token.ThrowIfCancellationRequested(); return(patientCountAggregator.Aggregate(partials)); }
async Task <HashSet <string> > GetPatientSetAsync(IReadOnlyCollection <LeafQuery> queries, CancellationToken token) { var all = queries.Select(q => GetPartialContext(q, token)); var partials = await Task.WhenAll(all); token.ThrowIfCancellationRequested(); return(patientCountAggregator.Aggregate(partials)); }