异步/并行组合 [英] Async/Parallel Combined

查看:67
本文介绍了异步/并行组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些在F#中将异步与并行相结合的示例.这是一个MSDN示例: http://msdn.microsoft .com/en-us/library/dd233250(v = vs.120).aspx 这不是对线程的低效率使用吗?您为什么要对许多可能长时间运行的IO类型操作使用新线程.基本上不是创建只坐在那里等待的线程吗?

I have seen some examples of combining async with parallel in F#. Here's an MSDN sample: http://msdn.microsoft.com/en-us/library/dd233250(v=vs.120).aspx Isn't that an inefficient use of threads? Why would you want to use new threads for many, potentially long running, IO type operations. Isn't that basically creating threads that will just sit there and wait?

推荐答案

快速解答:异步工作流是轻量级的用户空间线程.您可以创建大量这样的异步工作流,假设它具有异步,并发或并行计算,这可以帮助以更自然的方式构建程序.一个本地线程可以运行任意数量的异步工作流,并且异步工作流可以从一个本地线程转移到另一个本地线程.这使您的程序可以有效地利用本机线程.

Quick answer: Asynchronous workflows are lightweight, user-space threads. You can create large numbers of such asynchronous workflows, which can help to structure your program in a more natural manner, assuming it has asynchronous, concurrent or parallel computations. A single native thread can run arbitrary numbers of asynchronous workflows and asynchronous workflows can transfer from one native thread to another. This allows your program to make effective and efficient use of native threads.

异步工作流的本质特征是,异步工作流中的操作可以被阻塞,以等待长时间运行(异步)操作的结果,而不会阻塞本机线程.这样一来,一个本机线程即可运行多个异步工作流(一次运行一个).

The essential feature of asynchronous workflows is that an operation in an asynchronous workflow can be blocked waiting for the result of a long running (asynchronous) operation without blocking a native thread. This allows a single native thread to run multiple asynchronous workflows (one at a time).

在链接到的特定示例中,Async.Parallel用于并行执行多个长时间运行的操作.异步工作流的使用使程序的并行化版本在结构上变得简单,同时避免了使用多个(昂贵的)本地线程. 因此,答案是否定的:这不会创建大量仅等待的本机线程.发生的是(相对)少量的本机线程将运行那些异步工作流.当特定的异步工作流被阻塞,等待长时间运行的操作时,本机线程将开始运行其他可以继续执行的异步工作流.

In the particular example that you linked to, Async.Parallel is used to execute multiple long running operations in parallel. Use of asynchronous workflows makes the parallelized version of the program structurally simple, while avoiding the use of multiple (expensive) native threads. So, the answer is no: This doesn't create a large number of native threads that would simply wait. What happens is that a (relatively) small number of native threads will run those asynchronous workflows. When a particular asynchronous workflow becomes blocked, waiting for a long running operation, the native thread starts running some other asynchronous workflow that is ready to be continued.

关于并行编程,特别是异步工作流存在问题.具体而言,异步工作流以(半)抢占方式执行.基本上,将执行异步工作流,以便在执行了一些操作之后,即使不阻止或等待长时间运行的操作,一个特定的异步工作流也将入队,而另一个工作流则被出队执行.这对于并行性能而言是灾难性的,因为这意味着您的程序将使用与准备好的工作流数量成比例的内存,该数量可能比CPU内核的数量大得多.为了最大化并行性能,最好使用协作调度.这是我为F#创建 Hopac 库的原因之一.

Regarding parallel programming, in particular, there is a problem with asynchronous workflows. Specifically, asynchronous workflows are executed in a (semi) preemptive manner. Basically, asynchronous workflows are executed so that after performing some number of operations, a particular asynchronous workflow is enqueued, even if it doesn't block or wait for a long running operation, and another workflow is dequeued for execution. This is disastrous for parallel performance, because it means that your program will use memory proportional to the number of ready workflows, which can be much larger than the number of CPU cores. For maximizing parallel performance, it is better to use cooperative scheduling. This is one of the reasons I created the Hopac library for F#.

这篇关于异步/并行组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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