任务并行库与异步工作流 [英] Task Parallel Library vs Async Workflows

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

问题描述

我有一些用 c# 编写的东西,可以执行并发代码,大量使用任务并行库(任务和未来延续链).

I have some stuff written in c# that executes concurrent code, making heavy use of the Task Parallel Library (Task and Future continuation chains).

我现在将其移植到 F# 并试图找出使用 F# 异步工作流与 TPL 中的构造的优缺点.我倾向于 TPL,但我认为这两种方式都可以完成.

I'm now porting this to F# and am trying to figure out the pros and cons of using F# Async workflows vs. the constructs in the TPL. I'm leaning towards TPL, but I think it could be done either way.

有没有人有关于用 F# 编写并发程序的技巧和智慧可以分享?

Does anyone have tips and wisdom about writing concurrent programs in F# to share?

推荐答案

这个名字几乎概括了区别:异步编程与并行编程.但在 F# 中,您可以混合搭配.

The name pretty much sums up the difference: asynchronous programming vs. parallel programming. But in F# you can mix and match.

F# 异步工作流在您希望异步执行代码时很有用,即启动任务而不是等待最终结果.最常见的用法是 IO 操作.让您的线程在空闲循环中等待您的硬盘完成写入会浪费资源.

F# async workflows are helpful when you want to have code execute asynchronously, that is starting a task and not waiting around for the final result. The most common usage of this is IO operations. Having your thread sit there in an idle loop waiting for your hard disk to finish writing wastes resources.

如果你异步开始写操作,你可以挂起线程,稍后通过硬件中断唤醒它.

If you began the write operation asynchronously you can suspend the thread and have it woken up later by a hardware interrupt.

.NET 4.0 中的任务并行库抽象了任务的概念 - 例如解码 MP3,或从数据库读取一些结果.在这些情况下,您实际上想要计算结果,并且在稍后的某个时间点等待操作的结果.(通过访问 .Result 属性.)

The Task Parallel Library in .NET 4.0 abstracts the notion of a task - such as decoding an MP3, or reading some results from a database. In these situations you actually want the result of the computation and at some point in time later are waiting for the operation's result. (By accessing the .Result property.)

您可以轻松混合和匹配这些概念.例如在 TPL 任务对象中执行所有 IO 操作.对于程序员来说,您已经抽象了处理"额外线程的需要,但在幕后您却在浪费资源.

You can easily mix and match these concepts. Such as doing all of your IO operations in a TPL Task object. To the programmer you have abstracted the need to 'deal with' that extra thread, but under the covers you're wasting resources.

同样,您可以创建一系列 F# 异步工作流并并行运行它们 (Async.Parallel),但随后您需要等待最终结果 (Async.RunSynchronously).这使您无需显式启动所有任务,但实际上您只是在并行执行计算.

Like wise you can create a series of F# async workflows and run them in parallel (Async.Parallel) but then you need to wait for the final result (Async.RunSynchronously). This frees you from needing to explicitly start all the tasks, but really you are just performing the computation in parallel.

根据我的经验,我发现 TPL 更有用,因为通常我想并行执行 N 个操作.但是,F# 异步工作流非常适合在幕后"发生某些事情时,例如反应式代理或邮箱类型的事情.(你向某物发送一条消息,它会处理它并将其发回.)

In my experience I find that the TPL is more useful because usually I want to execute N operations in parallel. However, F# async workflows are ideal when there is something that is going on 'behind the scenes' such as a Reactive Agent or Mailbox type thing. (You send something a message, it processes it and sends it back.)

希望有所帮助.

这篇关于任务并行库与异步工作流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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