scala中有快速并发语法糖的实现吗?例如。映射减少
与演员传递信息很棒。但我想要更简单的代码。
示例(伪代码)
val splicedList:List[List[Int]]=biglist.partition(100)
val sum:Int=ActorPool.numberOfActors(5).getAllResults(splicedList,foldLeft(_+_))
spliceIntoParts 将一个大列表变成 100 个小列表的 numberofactors 部分,创建一个使用 5 个参与者的池,并在作业完成后接收新作业 getallresults 使用列表上的方法。所有这一切都是通过在后台传递消息来完成的。其中可能是 getFirstResult,计算第一个结果,并停止所有其他线程(例如破解密码)
Passing messages around with actors is great. But I would like to have even easier code.
Examples (Pseudo-code)
val splicedList:List[List[Int]]=biglist.partition(100)
val sum:Int=ActorPool.numberOfActors(5).getAllResults(splicedList,foldLeft(_+_))
where spliceIntoParts turns one big list into 100 small lists
the numberofactors part, creates a pool which uses 5 actors and receives new jobs after a job is finished
and getallresults uses a method on a list. all this done with messages passing in the background. where maybe getFirstResult, calculates the first result, and stops all other threads (like cracking a password)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 2.8.1 中包含的 Scala Parallel 集合,您将能够执行以下操作:
并且代码将自动并行完成。常规集合库中的其他方法也正在并行化。
目前,2.8.RC2 已经非常接近了(本周或下周),我猜 2.8 最终版本将在几周后发布。如果您使用 2.8.1 nightlies,您将能够尝试并行收集。
With Scala Parallel collections that will be included in 2.8.1 you will be able to do things like this:
and the code will automatically be done in parallel. Other methods found in the regular collections library are being parallelized as well.
Currently, 2.8.RC2 is very close (this or next week), and 2.8 final will come in a few weeks after, I guess. You will be able to try parallel collections if you use 2.8.1 nightlies.
您可以使用 Scalaz 的并发功能来实现您想要的目的。
让这个变得更漂亮是很容易的(即编写一个函数mapReduce,将分割和折叠全部合并在一起)。此外,List 上的 parMap 不必要地严格。您需要在整个列表准备好之前开始折叠。更像是:
You can use Scalaz's concurrency features to achieve what you want.
It would be pretty easy to make this prettier (i.e. write a function mapReduce that does the splitting and folding all in one). Also, parMap over a List is unnecessarily strict. You will want to start folding before the whole list is ready. More like:
与使用 future 创建 actor 相比,您可以用更少的开销来完成此操作:
您必须处理分解问题并编写“future”块并将其重新组合成最终答案,但它确实需要并行执行一堆小代码块很容易做到。
(请注意,左侧折叠中的
_()
是 future 的 apply 函数,这意味着“给我你正在并行计算的答案!”,并且它会阻塞,直到答案可用为止.)并行集合库会自动分解问题并为您重新组合答案(就像 Clojure 中的
pmap
一样);这还不是主要 API 的一部分。You can do this with less overhead than creating actors by using futures:
You have to handle decomposing the problem and writing the "future" block and recomposing it in to a final answer, but it does make executing a bunch of small code blocks in parallel easy to do.
(Note that the
_()
in the fold left is the apply function of the future, which means, "Give me the answer you were computing in parallel!", and it blocks until the answer is available.)A parallel collections library would automatically decompose the problem and recompose the answer for you (as with
pmap
in Clojure); that's not part of the main API yet.我不等待 Scala 2.8.1 或 2.9,最好编写自己的库或使用另一个库,所以我做了更多谷歌搜索并发现了这个:akka
http://doc.akkasource.org/actors
它有一个带有方法的对象 future
,但是 http://scalablesolutions.se/akka/api/akka-core-0.8.1/
根本没有文档。那很糟糕。
但好的一点是 akka 的 actor 比 scala 的原生 actor 更精简
有了所有这些库(包括 scalaz),如果 scala 本身最终能够正式合并它们,那就太好了
I'm not waiting for Scala 2.8.1 or 2.9, it would rather be better to write my own library or use another, so I did more googling and found this: akka
http://doc.akkasource.org/actors
which has an object futures with methods
but http://scalablesolutions.se/akka/api/akka-core-0.8.1/
has no documentation at all. That's bad.
But the good part is that akka's actors are leaner than scala's native ones
With all of these libraries (including scalaz) around, it would be really great if scala itself could eventually merge them officially
在 2010 年 Scala Days 上,Aleksandar Prokopec(他在 EPFL 从事 Scala 工作)进行了一次非常有趣的演讲,主题是 并行集合。这可能会在 2.8.1 中出现,但您可能需要等待更长的时间。我看看是否能得到演示文稿本身。链接到这里。
我们的想法是拥有一个集合框架,它通过完全按照您的建议进行集合处理,但对用户透明。理论上您所要做的就是将导入从 scala.collections 更改为 scala.parallel.collections。显然,您仍然需要做一些工作来看看您正在做的事情是否真的可以并行化。
At Scala Days 2010, there was a very interesting talk by Aleksandar Prokopec (who is working on Scala at EPFL) about Parallel Collections. This will probably be in 2.8.1, but you may have to wait a little longer. I'll lsee if I can get the presentation itself. to link here.
The idea is to have a collections framework which parallelizes the processing of the collections by doing exactly as you suggest, but transparently to the user. All you theoretically have to do is change the import from scala.collections to scala.parallel.collections. You obviously still have to do the work to see if what you're doing can actually be parallelized.