4

Trying to understand server-side async/await use. My understanding is that it is only useful when the thread can be freed. If I have:

[HttpGet]
public async Task<IEnumerable<MyObject>> Get()
{        
     return await Task<IEnumerable<MyObject>>.Run(() => DoThingsAndGetResults());
}

IEnumerable<MyObject> DoThingsAndGetResults()
{
    // ...Do some CPU computations here...

    IEnumerable<MyObject> result = myDb.table.Select().ToList(); // entity framework

    // ...Do some CPU computations here...

    return result;
}

Will the thread ever be freed? Is Async/Await useless in this case? Is the only way to gain benefit from async/await is if I make some bigger changes and do this:

[HttpGet]
public async Task<IEnumerable<MyObject>> Get()
{        
     return await DoThingsAndGetResultsAsync();
}

async IEnumerable<MyObject> DoThingsAndGetResultsAsync()
{
    // ...Do some CPU computations here...

    IEnumerable<MyObject> result = await myDb.table.Select().ToListAsync(); // entity framework

    // ...Do some CPU computations here...

    return result;
}
Steve
  • 1,584
  • 2
  • 18
  • 32
  • Here's a good video from TechEd about async programming http://channel9.msdn.com/events/TechEd/NorthAmerica/2014/DEV-B362#fbid= – Mike Norgate May 29 '14 at 06:49
  • Read also this: http://www.asp.net/web-forms/tutorials/aspnet-45/using-asynchronous-methods-in-aspnet-45 and http://www.hanselman.com/blog/TheMagicOfUsingAsynchronousMethodsInASPNET45PlusAnImportantGotcha.aspx – Tigran May 29 '14 at 06:52

3 Answers3

5

Async methods are mostly useful when you have to wait for IO operation (reading from a file, querying the database, receiving response from a web server).

It is ok to use async when, for example, your database provider supports async querying like Entity Framework 6. Though it is not very useful for CPU bound operations (calculations and so on).

See related answers at ASP.NET MVC4 Async controller - Why to use?

Community
  • 1
  • 1
4

If what you want to achieve is to free threads, the second example is better. The first one holds a thread throughout the operation.

Explanation

All that async-await does is to simply helps you write code that runs asynchronously but "looks" synchronous.

There are 2 reasons for asynchronous code:

  1. Offloading. Used mostly in GUI threads, or other "more important" threads". (Releasing threads while waiting for CPU operations to complete).
  2. Scalability. Used mainly in the server-side to reduce resource usage. (Releasing threads while waiting for IO to complete).

Unsurprisingly the reasons match your examples.

In the first example you run DoThingsAndGetResults on a different thread (using Task.Run) and waiting for it to complete asynchronously.The first thread is being released, but the second one doesn't.

In the second one you only use 1 thread at a time, and you release it while waiting for IO (Entity Framework).

Community
  • 1
  • 1
i3arnon
  • 113,022
  • 33
  • 324
  • 344
  • So (I need it spelt out) the async/await usage in the first code block is useless, since it offers neither offloading or scalability? – Steve May 29 '14 at 09:11
  • 2
    @Steve It does offer offloading, but for no apparent reason, so yeah... it's useless. – i3arnon May 29 '14 at 09:15
1

One of the benefits when using An async operation for long running io operations server Side, like database calls can be, is that you free up The thread The request was processed on, so that web server has more threads available To proces new requests. Mind that this is true when using The default taskscheduler, that schedules tasks on An available thread from The threadpool. When using another taskscheduler, The effect could be different...