Why use Async/await all the way down

前端 未结 5 1193
情深已故
情深已故 2020-11-29 07:12

I would like to get some clarification on what is the added benefit of using of Await and Async all the way down.

If my application is calling await Func1()

5条回答
  •  一整个雨季
    2020-11-29 08:02

    The major benefit is the fact that awaiting an asynchronous method returns the worker thread to the pool to be used in other calls (web requests to your .NET MVC web app, for example). The asynchronous work is done on an IO completion thread. When the awaited method finishes, another worker thread will collect the results and resume execution. This prevents worker thread pool exhaustion and allows your application to handle more load (CPU, memory, and network throughput depending).

    As for "await all the way down", that seems like an issue to me. Typically await is associated to an external resource (DB call, HTTP request, etc.) that your application must wait on. If you await code that doesn't have external IO dependencies, you're creating overhead that isn't needed. It's possible to have multiple awaits in an async method chain, but awaiting some code that itself calls await but has no other external IO dependency is not good and will just add callback/compiler overhead.

提交回复
热议问题