What is the use of an async await method that only call one liners async await methods? How many threads do they span? How much CPU you can end up consuming?
Hi, lately I'm seeing a lot of methods using the async and await keywords. I still cannot wrap my head around the use of them and the underlying behavior as well as their advantage when applied like below. I ahve also obseved a problem with the CPU (going 100%) when they use it for everything and I'm seeing thread.run as the culprit for cpu utilization for some of this cases.
myController.cs
[HttpPost]
[ProducesResponseType(body)]
public async Task<IActionResult> Post([FromBody]Body body){
_MyClassService.sendMessage(body);
return Ok(body);
}
My Service class
Myclass.cs
private async void sendMessage(){
await SendToProperChannel();
}
private async Task SendToProperChannel(){
await DoWorkInProperChannel();
}
private Task<int> DoWorkInProperChannel(){
await SomethingThatTakes5Seconds();
return 1+1;
}
According to my little knowledge.
We are making the controller request async in order to not block main threads, allowing to receive higher throughput at the controller level.
We are making also all the functions async to not block the upper threads that call those methods.
I also understand that since we have so many awaits, everyone returns a task but also a thread is handling that work waiting for the completion of this.
I'm still not clear on the thread number but I believe we are spawning more than 1 thread to fulfill a single request.
My questions.
What is the use of having one liners await if the upper method cannot continue without the response? The API can, in fact, receive more throughput, but down from there everything will be kind of sync, also spawning more threads just for jumping between methods in my class doesn't seem healthy.
If I only awaiting on one liners, in this specific code, the result would not be the same as removing the awaits?
All methods will also handle more throughput, since they don't block, but will also spawn more threads?
How is the syntax for starting an async method and waiting for the response way down inside the same method.
Example:
private async Task<int> myAsyncWorkerMethod(){
var myNeededValue=methodThatINeed();
var mySecondValue=methodWithSecondValue();
await myNeededValue;
await mySecondValue;
return myNeededValue+mySecondValue;
}
What is the benefit from async and await against having just a thread pool that handles all downstream calls, which you can easily control its size and can easily detect thread starvation and queue depth? (I might be using some java terms in here that might not apply)
Thoughts
In other lenguages I have seen the repercussions of thread context change, if your CPU is not powerful enough or you have thread starvation because of the thread pool size, does this .net uncontrolled thread pool would not have the same repercussions and just use the CPU as much as he wants?
You don't need to create an async invocation if you need them to return the response for the await statement. making asynchronous is only needed when you need to do some work on the above method?
Even if you are awaiting method results from another class unless you don't want to block the main thread you should also not wait for it.
Thanks in advance
See Question&Answers more detail:
os