Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Convenience Parallel Processing methods for Ruby
Ruby
branch: master

This branch is 19 commits behind bruceadams:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
Gemfile
LICENSE
README.md
Rakefile
pmap.gemspec

README.md

pmap Build Status

This Ruby gem adds two methods to any Enumerable (notably including any Array). The two added methods are:

  • pmap parallel map
  • peach parallel each

Threading in Ruby has limitations.

Matz Ruby 1.8.* uses green threads. All Ruby threads are run within a single thread in a single process. A single Ruby program will never use more than a single core of a mutli-core machine.

Matz Ruby 1.9.* uses native threads. Each Ruby thread maps directly to a thread in the underlying operating system. In theory, a single Ruby program can use multpile cores. Unfortunately, there is a global interpreter lock GIL that causes single-threaded behavior.

JRuby also uses native threads. JRuby avoids the global interpreter lock, allowing a single Ruby program to really use multiple CPU cores.

Threading useful for remote IO, such as HTTP

Despite the Matz Ruby threading limitations, IO bound actions can greatly benefit from multi-threading. A very typical use is making multiple HTTP requests in parallel. Issuing those requests in separate Ruby threads means the requests will be issued very quickly, well before the responses start coming back. As responses come back, they will be processed as they arrive.

Example

Suppose that we have a function get_quote that calls out to a stock quote service to get a current stock price. The response time for get_quote ranges averages 0.5 seconds.

stock_symbols = [:ibm, :goog, :appl, :msft, :hp, :orcl]

# This will take about three seconds;
# an eternity if you want to render a web page.
stock_quotes = stock_symbols.map {|s| get_quote(s)}

# Replacing "map" with "pmap" speeds it up.
# This will take about half a second;
# however long the single slowest response took.
stock_quotes = stock_symbols.pmap {|s| get_quote(s)}
Something went wrong with that request. Please try again.