Ruby有什么类似于Clojure的pmap的并行处理的结构吗? [英] Does Ruby have any construct similar to Clojure's pmap for parallel processing?

查看:123
本文介绍了Ruby有什么类似于Clojure的pmap的并行处理的结构吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想决定是否在Ruby或Clojure中实现应用程序。其中两个要求涉及并行处理:

I'm trying to decide whether to implement an application in Ruby or Clojure. Two of the requirements involve parallel processing:


  1. 应用程序必须进行并行调用,通过并行调用获取XML提要和其他类型的数据互联网。许多这样的调用都是做的,并且序列化调用是低效的。

  1. The app must make parallel calls to fetch XML feeds and other types of data over the internet. Many such calls are made, and serializing the calls is inefficient.

这些调用的响应应该理想地并行处理。处理主要是指将原始XML转换为更小的结构化数据(Ruby哈希或Clojure映射),并将其插入MySQL数据库或CouchDB数据库。

The responses to those calls ought ideally to be processed in parallel. Processing mainly means transforming raw XML down to a much smaller piece of structured data (a Ruby hash or Clojure map) and inserting that into a MySQL database or CouchDB database.

我知道Ruby比Clojure好多了,但如果这是Clojure的项目,我就是使用它。

I know Ruby a lot better than Clojure but if this is the right sort of project for Clojure I am all for using it.

Clojure的 pmap 函数似乎是这两个要求的理想选择。我想知道一些Ruby库或者特性是否有类似干净和容易的方式来执行类似上面的并行处理任务。

Clojure's pmap function seems ideal for these two requirements. I'm wondering if some Ruby library or feature had a similarly clean and easy way of doing parallel processing tasks like the above.

推荐答案

这里有一个简单的例子。注意,没有限制它一次创建的线程数量,所以如果你运行很多线程,你可能想创建一些类型的线程池。

Here's a simple little example of one way to do this. Note that there's nothing limiting the number of threads it creates at once, so you might want to create some sort of thread pool if you're running lots of threads.

[1,2,3].map{|x| Thread.start{x+1}}.map{|t| t.join.value}

这篇关于Ruby有什么类似于Clojure的pmap的并行处理的结构吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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