这是异步等待方式的正确图示吗? [英] Is this a correct diagram of how async-await works?

查看:94
本文介绍了这是异步等待方式的正确图示吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将尝试在 async - await 上进行演讲,并创建一个试图显示可能的执行顺序的流程图。

I'm going to be trying to give a talk on async-await and I'm creating a flow chart that attempts to show the possible orders of execution.

我试图以此为基础


异步方法的开始就像其他
方法一样被执行。也就是说,它会同步运行,直到遇到 await(或
引发异常)为止。

The beginning of an async method is executed just like any other method. That is, it runs synchronously until it hits an "await" (or throws an exception).

await关键字是事物可以异步进行的地方。 Await是
,就像一元运算符一样:它只接受一个参数,一个awaitable(
awaitable是一个异步操作)。 Await检查可等待的
是否已经完成;如果awaitable已完成
,则该方法将继续运行
(同步,就像常规方法一样)。

The "await" keyword is where things can get asynchronous. Await is like a unary operator: it takes a single argument, an awaitable (an "awaitable" is an asynchronous operation). Await examines that awaitable to see if it has already completed; if the awaitable has already completed, then the method just continues running (synchronously, just like a regular method).

如果 await看到awaitable尚未完成,则它异步执行
。它告诉awaitable在
方法完成时运行其余部分,然后从async方法返回。

If "await" sees that the awaitable has not completed, then it acts asynchronously. It tells the awaitable to run the remainder of the method when it completes, and then returns from the async method.

稍后,当awaitable完成时,它将执行async方法的其余
。如果您正在等待内置的await(例如
a任务),则异步方法的其余部分将在
上下文上执行,该上下文在 await返回之前捕获。 p>

Later on, when the awaitable completes, it will execute the remainder of the async method. If you’re awaiting a built-in awaitable (such as a task), then the remainder of the async method will execute on a "context" that was captured before the "await" returned.

来自 http://blog.stephencleary.com/2012/02/async-and-await.html

推荐答案

usr的答案基本上是正确的,尽管我认为它使线程和任务之间的类比过于强烈。一个任务不必像另一个线程一样。记住,线程是工人,任务是工作。您可以在待办事项列表上列出一百项事情,而无需雇用任何工人来做。尽量不要将任务视为轻量级工作者,因为事实并非如此。他们是需要完成的工作。

The answer of usr is basically correct, though I think it makes too strong an analogy between threads and tasks. A task need not be anything like another thread. Remember, threads are workers, tasks are jobs. You can have a hundred things on your to-do list without hiring any workers to do them. Try to not think of tasks as lightweight workers, because they are not. They are jobs that need to be done; what worker does them is up to the code that handed you the task.

您的图表起初很好,但是在呼叫者完成了所有独立工作之后,您的关系图了吗? ?不管它是什么,呼叫者的延续都可以。如果这种延续涉及到工作,那么它确实起作用。其中一些工作可能是安排任务在当前线程上运行。

Your diagram starts off fine but it goes off the rails at "does the caller finish all independent work?" The continuation of the caller is, well, whatever it is. If that continuation involves doing work, it does work. Some of that work might be scheduling tasks to run on the current thread. Some of that work might be keeping the UI responsive.

此外,别忘了调用者的线程可能会终止,并且任务的继续可以安排到另一个线。

Also, don't forget that the caller's thread could be terminated and the continuation of the task could be scheduled to another thread.

这里可能发生很多事情。在不了解调用者到底在做什么以及调用者的线程上下文是什么的情况下,无法说出等待返回后立即发生什么。

There are many, many things that can happen here; without understanding what exactly the caller is doing and what the thread context of the caller is, it is impossible to say what happens immediately after the await returns.

这篇关于这是异步等待方式的正确图示吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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