对ASP.NET MVC上的异步/等待的深入了解 [英] Deep understanding of async / await on ASP.NET MVC

查看:102
本文介绍了对ASP.NET MVC上的异步/等待的深入了解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在MVC控制器上执行异步操作时,尤其是在处理I/O操作时,我不完全了解幕后情况.假设我有一个上传操作:

I don't understand exactly what is going on behind the scenes when I have an async action on an MVC controller especially when dealing with I/O operations. Let's say I have an upload action:

public async Task<ActionResult> Upload (HttpPostedFileBase file) {
  ....
  await ReadFile(file);

  ...
}

据我所知,这些是发生的基本步骤:

From what I know these are the basic steps that happen:

  1. 从线程池中偷窥并分配了一个新线程来处理传入的请求.

  1. A new thread is peeked from threadpool and assigned to handle incomming request.

当等待命中时,如果调用是I/O操作,则原始线程将返回到池中,并将控制权转移到所谓的IOCP(输入输出完成端口).我不明白的是为什么该请求仍然存在并等待答复,因为最终呼叫的客户端将等待我们的请求完成.

When await gets hit, if the call is an I/O operation then the original thread gets back into pool and the control is transfered to a so-called IOCP (Input output completion port). What I do not understand is why the request is still alive and waits for an answer because in the end the calling client will wait for our request to complete.

我的问题是:谁/何时/如何等待完全阻止?

My question is: Who / when / how does this wait for complete blocking occurs?

注意:我看到了博客文章 没有主题 ,这对于GUI应用程序很有意义,但是对于这种服务器端方案,我不了解.真的.

Note: I saw the blog post There Is No Thread, and it makes sense for GUI applications, but for this server side scenario I don't get it. Really.

推荐答案

'net上有一些不错的资源,它们对此进行了详细描述.我写了 MSDN文章,从较高的角度对此进行了描述.

There's some good resources on the 'net that do describe this in detail. I wrote an MSDN article that describes this at a high level.

我不明白的是为什么请求仍然存在并等待回答,因为最终调用方客户端将等待我们的请求完成.

What i do not understand is why the request is still alive and waits for an answer because in the end the calling client will wait for our request to complete.

它仍然存在,因为ASP.NET运行时尚未完成它.完成请求(通过发送响应)是一个明确的动作;并不是请求会自己完成.当ASP.NET看到控制器动作返回Task/Task<T>时,它将在该任务完成之前不会完成请求.

It's still alive because the ASP.NET runtime has not yet completed it. Completing the request (by sending the response) is an explicit action; it's not like the request will complete on its own. When ASP.NET sees that the controller action returns a Task/Task<T>, it will not complete the request until that task completes.

我的问题是:谁/何时/如何等待完全阻止?

My question is: Who / when / how does this wait for complete blocking occurs ?

什么都没有等待.

以这种方式思考:ASP.NET具有正在处理的当前请求的集合.对于给定的请求,请求一旦完成,就会发出响应,然后将该请求从集合中删除.

Think of it this way: ASP.NET has a collection of current requests that it's processing. For a given request, as soon as it's complete, the response is sent out and then that request is removed from the collection.

关键是它是请求的集合,而不是线程.这些请求中的每一个在任何时间点都可能有线程运行,也可能没有线程运行.同步请求始终只有一个线程(同一线程).异步请求可能有一段时间没有线程.

The key is that it's a collection of requests, not threads. Each of those requests may or may not have a thread working on it at any point in time. Synchronous requests always have a single thread (the same thread). Asynchronous requests may have periods when they don't have threads.

注意:我看到了这个线程: http://blog.stephencleary.com/2013/11/there-is-no-thread.html ,它对于GUI应用程序很有意义,但对于这种服务器端方案,我不了解.

Note: i saw this thread: http://blog.stephencleary.com/2013/11/there-is-no-thread.html and it makes sense for GUI applications but for this server side scenario I don't get it.

ASP.NET应用程序的无线程I/O方法与GUI应用程序的无线程方法完全相同.

The threadless approach to I/O works exactly the same for ASP.NET apps as it does for GUI apps.

最终,文件写入将完成,(最终)完成从ReadFile返回的任务.通常,此完成任务"工作是通过线程池线程完成的.由于任务现在已完成,因此Upload动作将继续执行,导致该线程进入请求上下文(也就是说,现在有一个线程再次执行该请求). Upload方法完成后,从Upload返回的任务就完成了,ASP.NET将写出响应并将请求从其集合中删除.

Eventually, the file write will complete, which (eventually) completes the task returned from ReadFile. This "completing of the task" work is normally done with a thread pool thread. Since the task is now complete, the Upload action will continue executing, causing that thread to enter the request context (that is, there is now a thread executing that request again). When the Upload method is complete, then the task returned from Upload is complete, and ASP.NET writes out the response and removes the request from its collection.

这篇关于对ASP.NET MVC上的异步/等待的深入了解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆