Nested Async / Await should not scale

I have the following (simplified) code:

public async Task GetData(DomainObject domainObject, int depth) { // This async operation is really quick, and there usually like five. IEnumerable<TierOne> tierOnes = await domainObject.GetTierOnesAsync(); var tierOneTasks = tierOnes.Select(async tierOne => { // This async operation is really quick and there usually like three. IEnumerable<TierTwo> tierTwos = await tierOne.GetTierTwosAsync(); if (depth <= TierTwoDepth) return; var tierTwoTasks = tierTwos.Select(async tierTwo => { // This async operation is usually fast, and there usually >= 100. IEnumerable<TierThree> tierThrees = await tierTwo.GetTierThreesAsync(); if (depth <= TierThreeDepth) return; var tierThreeTasks = tierThrees.Select(async tierThree => { // This async operation is SLOW, and there usually.. 50? await tierThree.GetTierFoursAsync(); }); await Task.WhenAll(tierThreeTasks.ToArray()); }); await Task.WhenAll(tierTwoTasks.ToArray()); }); await Task.WhenAll(tierOneTasks.ToArray()); } 

Based on what I saw, it doesn't seem to scale very well. All Async operations are valid asynchronous operations, which means that they are all I / O.

Am I using Async / Await incorrectly for this scenario? Based on my current observations, this does not scale compared to what I would expect. Will TPL DataFlow be my solution?

+5
source share
1 answer

For a single GetData call, nested async / await calls will not introduce any concurrency. You retrieve all tierOnes, then all tierTwos for tierOne- # 1, then all tierThrees for tierTwo- # 1, etc. All are executed sequentially (although there may be several concurrency in GetTier * Async methods).

If you need concurrent queries, then the TPL data stream is indeed the best solution.

0
source

All Articles