最佳实践并行使用异步工作流程 [英] Best practices to parallelize using async workflow

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

问题描述

可以说,我想凑一个网页,并提取一些数据。我想最有可能这样写:

Lets say I wanted to scrape a webpage, and extract some data. I'd most likely write something like this:

let getAllHyperlinks(url:string) =
    async {  let req = WebRequest.Create(url)
             let! rsp = req.GetResponseAsync()
             use stream = rsp.GetResponseStream()             // depends on rsp
             use reader = new System.IO.StreamReader(stream)  // depends on stream
             let! data = reader.AsyncReadToEnd()              // depends on reader
             return extractAllUrls(data) }                    // depends on data

让!告诉F#在另一个线程来执行code,然后将结果绑定到一个变量,并继续处理。上面的示例使用两个让语句:一是获得响应,以及​​一个用于读取所有的数据,所以它产生至少有两个线程(请纠正我,如果我错了)

The let! tells F# to execute the code in another thread, then bind the result to a variable, and continue processing. The sample above uses two let statements: one to get the response, and one to read all the data, so it spawns at least two threads (please correct me if I'm wrong).

尽管上面的工作流程派生多个线程,执行的顺序是串行因为在工作流的每个项目依赖于previous项。它不是真的有可能进一步评估任何项目下的工作流程,直到其他线程返回。

Although the workflow above spawns several threads, the order of execution is serial because each item in the workflow depends on the previous item. Its not really possible to evaluate any items further down the workflow until the other threads return.

有没有有任何好处多个让!在code以上?

Is there any benefit to having more than one let! in the code above?

如果不是,怎么会变成这样code需要改变,以利用多重让!语句?

If not, how would this code need to change to take advantage of multiple let! statements?

推荐答案

关键是我们的的产卵的新主题。在工作​​流的整个过程中,有从线程池被消耗的1或0的活动线程。 (一个例外,直到第一个!时,code上做了一个Async.Run用户线程中运行)。让!让去一个线程,而异步操作是在海上,然后从线程池时操作返回拿起一个线程。在(性能)的优点是对线程池少pressure(当然,主要的用户优点是简单的编程模型 - 一百万次胜于一切的BeginFoo / EndFoo /回调的东西,否则你写的)。

The key is we are not spawning any new threads. During the whole course of the workflow, there are 1 or 0 active threads being consumed from the ThreadPool. (An exception, up until the first '!', the code runs on the user thread that did an Async.Run.) "let!" lets go of a thread while the Async operation is at sea, and then picks up a thread from the ThreadPool when the operation returns. The (performance) advantage is less pressure against the ThreadPool (and of course the major user advantage is the simple programming model - a million times better than all that BeginFoo/EndFoo/callback stuff you otherwise write).

另请参阅 http://cs.hubfs.net/forums/thread/8262。 ASPX

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

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