且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是使用异步与MVC5的优势在哪里?

更新时间:2023-02-25 08:33:57

异步操作仅当您正在执行I / O密集​​型操作时非常有用,如远程服务器调用。异步调用的好处是,在I / O操作期间,正在不使用ASP.NET工作线程。因此,这里的第一个例子是如何工作的:

The async actions are useful only when you are performing I/O bound operations such as remote server calls. The benefit of the async call is that during the I/O operation, no ASP.NET worker thread is being used. So here's how the first example works:


  1. 当一个请求到达的动作,ASP.NET需要从线程池中的线程并开始执行它。

  2. IdentityManager.Authentication.CheckPasswordAndSignIn 方法被调用。这是一个阻塞调用 - 整个呼叫工作线程被破坏时>

  1. When a request hits the action, ASP.NET takes a thread from the thread pool and starts executing it.
  2. The IdentityManager.Authentication.CheckPasswordAndSignIn method is invoked. This is a blocking call -> during the entire call the worker thread is being jeopardized.

和这里的第二个电话是如何工作的:

And here's how the second call works:


  1. 当一个请求到达的动作,ASP.NET需要从线程池中的线程并开始执行它。

  2. IdentityManager.Authentication.CheckPasswordAndSignInAsync 称为其立即返回。一个I / O完成端口被注册和ASP.NET辅助线程释放到线程池。

  3. 以后当操作完成后,I / O完成端口发出信号,另一个线程从线程池中提取完成返回视图。

  1. When a request hits the action, ASP.NET takes a thread from the thread pool and starts executing it.
  2. The IdentityManager.Authentication.CheckPasswordAndSignInAsync is called which returns immediately. An I/O Completion Port is registered and the ASP.NET worker thread is released to the thread pool.
  3. Later when the operation completes, the I/O Completion port is signaled, another thread is drawn from the thread pool to finish returning the view.

你可以在第二种情况下ASP.NET工作线程仅用于较短的时间段看。这意味着有池中可用更多的线程来服务于其他的请求。

As you can see in the second case ASP.NET worker threads are used only for a short period of time. This means that there are more threads available in the pool for serving other requests.

因此​​得出结论,使用异步操作,只有当你有一个内部的真正异步API。如果你做一个异步动作内阻塞调用,你杀了它的整体效益。

So to conclude, use async actions only when you have a true async API inside. If you make a blocking call inside an async action, you are killing the whole benefit of it.