“一直向下同步":那么,底部到底是怎么回事? [英] "Async All the Way Down": Well, what's all the way at the bottom?

查看:79
本文介绍了“一直向下同步":那么,底部到底是怎么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图完全理解async-await,而我理解的空白之一就是看到一直下来".我创建了一个async方法,该方法又被另一个async方法等调用,一直到我用模糊的术语理解的东西,例如"UI"或可以处理多个请求的Web服务器".我将如何用技术术语来描述什么是一路下跌"?

I'm trying to fully understand async-await and one of the gaps in my understanding is seeing what is "All the Way Down." I create an async method, it is called by another async method, etc., all the way down to something that I understand in vague terms like "a UI" or "a web server that can handle multiple requests". How would I describe in technical terms what is "all the way down"?

因此,让我们以Web服务器的第二个示例为例.假设我有一个

So let's take the second example of a web server. Say I have a controller action like

[HttpGet]
public async Task<IHttpActionResult> GetRecords()
{
    var records = await repository.GetRecordsFromDbAsync();
    return Ok(records);
}

在.NET源代码中,哪里可以找到能够使它被异步调用的一直向下"代码?

Where can I find in the .NET source code the "all the way down" code that enables this to be called asynchronously?

推荐答案

每种过程编程语言都是一系列函数/过程调用,而一个函数/过程则调用另一个函数/过程.可以使用调用图参见Wikipedida来表示从过程到过程的这种顺序调用.调用图.这些图通常显示了从顶部开始到底部进行过程调用的顺序.

Every procedural programming language is a series of function / procedure calls, with one function / procedure calling another. It is possible to represent this sequence of calls from procedure to procedure using a call graph see Wikipedida for a starting point for call graphs. These graphs generally show the sequence of procedural calls starting at the top and proceeding to the bottom.

我迅速生成了ASP .NET MVC应用程序的部分调用图的图.该图仅是ASP .NET MVC应用程序的部分表示,因为它省略了诸如操作系统(例如Windows),Web服务器(例如IIS)和ASP .NET的各种组件最初接收到请求之类的内容.负责处理HTTP请求通过ASP .NET Request处理管道时的处理.为了讨论的目的,可以省略这些事项.尽管值得注意的是,它们将位于调用图的顶部,因为它们处理处理HTTP请求的初始阶段,最终,在某个时候,ASP .NET最终会调用控制器的操作.

I quickly produced a diagram of a partial call graph for an ASP .NET MVC application. The diagram is only a partial representation for an ASP .NET MVC application because it omits things such as the initial receipt of the request by the operating system (eg. Windows), web server (eg. IIS) and various components of ASP .NET that are responsible for processing a HTTP request as it travels through the ASP .NET Request processing pipeline. These matters can can be omitted for the purposes of this discussion. Although it's worthwhile noting that they would sit at the top of the call graph, because they deal with the initial stages of handling the HTTP request, and ultimately, at some point ASP .NET ultimately invokes an action of a controller.

如您在图中所看到的,我代表了控制器的动作,该动作将由ASP .NET作为异步动作来调用.调用图的左侧是一系列异步过程调用.最终,它到达了您所要讨论的主题的方框中.

As you can see in the diagram I've represented the action of the controller that would be invoked by ASP .NET as an async action. On the left hand side of the call graph is a series of async procedure calls. Ultimately it arrives at the box that is the subject of your question.

该问题的答案与从头到尾"的概念是一致的,即我是一个异步方法".这个异步方法有什么作用?好吧,这取决于您想做什么?如果您正在读取或写入文件,那么这是一个异步调用来读取或写入文件.您要进行数据库查询吗?然后调用是进行该查询的异步方法.考虑到这一点,我想您可以说,通常最底层的是设备驱动程序方法,该方法异步执行IO访问.尽管它很可能是长时间运行的受计算限制的异步操作,但是您希望执行该操作,例如处理图像或视频文件.

The answer to the question that is consistent with the notion "All the way down" is that "I am an async method". What does this async method do? Well, that depends on what you want to do? If you're reading or writing a file then it's an async call to read or write a file. Are you making a database query? Then the call is to an async method that makes that query. With this in mind I guess you can say that often what is at the bottom will be a device driver method, that performs an IO access asynchronously. Although it could easily be a long running compute bound asynchronous operation that you wish to perform such as processing an image, or video file.

在这里也应该注意调用图的右侧.尽管通常您可能想完全调用异步方法,但是此调用图的右分支显示您不一定完全需要在底部调用异步方法.

It's worthwhile noting the right hand side of the call graph here too. Although often you may want to call async methods all the way down, the right hand branch of this call graph shows that you don't necessarily need to call an async method at the bottom at all.

这篇关于“一直向下同步":那么,底部到底是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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