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

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

问题描述

我有一些东西写在执行并发code,大量使用了任务并行库(任务和未来延续链)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?

推荐答案

名称pretty多总结了区别:异步编程与平行节目。但在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,或阅读从一个数据库中的一些结果。在这种情况下,你真正想要的计算结果,并在某个时间点后正在等待手术的结果。 (通过访问。结果属性。)

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.)

您可以轻松地混合和匹配这些概念。比如做所有的IO操作的TPL任务对象。对程序员已经提取了要对付这些额外的线程,但在幕后你在浪费资源。

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.

在我的经验,我发现,第三方物流更有用,因为平时我要并行执行个运算。然而,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.)

希望有所帮助。

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

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