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;
}
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?
If what you want to achieve is to free threads, the second example is better. The first one holds a thread throughout the operation.
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:
GUI
threads, or other "more important" threads". (Releasing threads while waiting for CPU operations 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With