简体   繁体   English

异步方法在ASP.NET(MVC)中的执行位置是什么?

[英]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. 许多文章(例如本文 )都说ASP.NET(MVC)中异步方法的优点是它们允许将线程释放到线程池,从而允许其他请求得到服务。 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. 在此上下文中async的主要用途是等待外部资源 - 例如,数据库(sql或no-sql),Web API(http)等。这些都不需要线程,因为它们不是基于CPU的操作。 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. await此处表示进程外工作 - 通常是网络。 They don't "run" anywhere, because they are not CPU operations. 它们不会“运行”任何地方,因为它们不是CPU操作。 When the place-holder tasks report completion, then the next part of the method can resume, typically via the captured sync-context. 当占位任务报告完成, 该方法的下一部分可以恢复,通常通过所捕获的同步上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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