简体   繁体   中英

Where does an async method execute in ASP.NET (MVC)?

Many articles (eg this one) say the advantage of async methods in ASP.NET (MVC) is that they allow release of threads to the thread pool which allow other requests to be serviced. If async methods do not use thread pool threads, where do they execute and why?

The main use of async in this context is to wait for external resources - for example, databases (sql or no-sql), web APIs (http), etc. There is no thread required for these, since they are not CPU-based operations. The work is resumed at some point after the data becomes available . Consider:

var cust = await someApi.GetCustomerAsync();
var account = await anotherApi.GetAccount(cust.AccountId);
return View(account);

The await here represent out-of-process work - typically network. They don't "run" anywhere, because they are not CPU operations. When the place-holder tasks report completion, then the next part of the method can resume, typically via the captured sync-context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM