我是异步的理解/计谋,它的工作原理和它的好处,是否正确? [英] Is my understanding of async/await, how it works and its benefits, correct?

查看:123
本文介绍了我是异步的理解/计谋,它的工作原理和它的好处,是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我断言我的异步的理解/等待一对夫妇的场合,经常与一些辩论至于我是否是正确的。我真的AP preciate它,如果任何人都可以证实或否认我的理解,并清理任何误解,让我做的不是S $ P $垫误传。

I've asserted my understanding of async/await on a couple of occasions, often with some debate as to whether or not I'm correct. I'd really appreciate it if anyone could either confirm or deny my understanding, and clear up any misconceptions so that I don't spread misinformation.

异步 / 等待为避免回调地狱而编写异步code的一种方式。正在执行异步方法的线程将返回线程池遇到一个时,等候,并拿起执行一次期待已久的操作完成。

async/await is a way of avoiding callback hell while writing asynchronous code. A thread that is executing an asynchronous method will return to the thread pool when it encounters an await, and will pick up execution once the awaited operation completes.

在JIT将拆分异步方法成离散的部分周围等待点,允许重新进入的方法,方法的状态preserved。在幕后,这涉及某种状态机。

The JIT will split an asynchronous methods into discrete parts around await points, allowing re-entry into the method with method's state preserved. Under the covers this involves some sort of state machine.

异步 / 伺机并不意味着任何形式的并发。使用异步 / 等待可能是完全单线程,同时还收获的所有优点,很多的方式编写的应用程序, Node.js的呢尽管有回调。不同的node.js,.NET是多线程的,以便通过具有异步 / 伺机,你得到的不好处-blocking IO不使用,同时具有多线程执行回调。

async/await does not imply any sort of concurrency. An application written using async/await could be entirely single-threaded while still reaping all the benefits, much the way that node.js does albeit with callbacks. Unlike node.js, .NET is multi-threaded so by having async/await, you get the benefits of non-blocking IO without using callbacks while also having multiple threads of execution.

异步 / 等待腾出线程做其他的事情在等待IO的完成。它也可以在与 TPL 以做的CPU结合使用约束工作在多个线程,或关闭UI​​线程。

async/await frees up threads to do other things while waiting for IO to complete. It can also be used in conjunction with the TPL to do CPU bound work on multiple threads, or off the UI thread.

为了从非阻塞IO,异步方法需要被建立在API的是的顶益实际上的利用它们由OS最终提供非阻塞IO的。

In order to benefit from non-blocking IO, the asynchronous methods need to be built on top of API's that actually take advantage of non-blocking IO which are ultimately provided by the OS.

这是争论的在我的理解中最大的一点。很多人认为,包装阻塞操作在工作和使用异步 / 等待将带来的性能提升。通过创建一个额外的线程来处理的操作,原来的线程返回到线程池,然后再恢复原来的方法完成任务后,所有这一切都发生的,而不是真正释放线程执行其他工作不必要的上下文切换。虽然这是没有那么多的误用异步 / 等待,因为它是第三方物流,这种心态似乎干从的误解异步 / 等待

This is the biggest point of contention in my understanding. A lot of people believe that wrapping a blocking operation in a Task and using async/await will bring about a performance increase. By creating an additional thread to handle an operation, returning the original thread to the thread pool, and then resuming the original method after the task completes, all that's occurring are unnecessary context switches while not really freeing up threads to do other work. While this isn't as much a misuse of async/await as it is the TPL, this mindset seems to stem from a misunderstanding of async/await.

推荐答案

这是pretty的很多正确的。

That's pretty much correct.

的几个注意事项虽然:

  • 的开始执行异步方法的线程调用者的线程,可能会或可能不会,是一个线程池主题。
  • 如果一个计谋达成,但awaitable(通常工作)已经完成了该线程将继续同步执行该方法的其余部分。
  • 将恢复运行的方法通常是线程池线程的线程,而是依赖于 SyncrhonizationContext 的TaskScheduler
  • 在JIT不在这里参与(不比平时多)。编译器是一个可以改变一个异步方法进入状态机。你可以看到,<一个href="http://tryroslyn.azurewebsites.net/#f:r/K4Zwlgdg5gBAygTxAFwKYFsDcAoUlaIoYB0AKgBYBOqAhgCb5k0gDWIOADsAEYA2YAYxgDezEDADC2AN4w5MbPJhc+gmMwQQhpZixgBZABQBKBUumKl8mgHcaYZDB2tiAEVSiEhgIwAGf8Y4SgC+2MFAAAA=">this TryRoslyn例如。
  • 这是真的,异步计谋并不一定意味着并发性,因为它可能是单线程的。然而,它仍然可以甚至只是一个单一的线程通过启动,并在同一时间等待多个异步操作并发
  • 异步计谋和TPL并非完全独立的部分。异步-的await是建立在TPL的顶部。这就是为什么它被称为基于任务的异步模式。
  • 在大多数真正的异步操作的I / O,而不是所有的都是。你也通常会延迟异步 Task.Delay 或使用异步同步结构,如 SemaphoreSlim.WaitAsync
  • The thread that starts executing an async method is the caller's thread which may, or may not, be a ThreadPool thread.
  • If an await is reached, but the awaitable (usually Task) is already completed the thread will continue executing the rest of the method synchronously.
  • The thread that resumes running the method is usually a ThreadPool thread, but that depends on the SyncrhonizationContext and TaskScheduler.
  • The JIT isn't involved here (not more than usual). The compiler is the one that turns an async method into the state machine. You can see that with this TryRoslyn example.
  • It's true that async-await doesn't necessarily imply concurrency as it could be single threaded. However, it can still be concurrent even with just a single thread by starting and awaiting multiple asynchronous operations at the same time.
  • async-await and the TPL are not completely separate parts. async-await is built on top of the TPL. That's why it's called the Task-based Asynchronous Pattern.
  • While most truly asynchronous operations are I/O, not all are. You also usually delay asynchronously with Task.Delay or use asynchronous synchronization constructs like SemaphoreSlim.WaitAsync.

这篇关于我是异步的理解/计谋,它的工作原理和它的好处,是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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