Coder Social home page Coder Social logo

pmap's Introduction

pmap Build Status Code Climate

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

  • pmap parallel map
  • peach parallel each
  • peach_with_index parallel each_with_index

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

Thread Count

The thread count defaults to 64 and is set based on $pmap_default_thread_count.

You can also set the thread count per call by passing it as an argument to the pmap and peach methods.

# Use the default thread count (64)
(1..128).peach { |i| sleep 1 } # Takes 2 seconds

# Use a thread count of 128
(1..128).peach(128) { |i| sleep 1 } # Takes 1 second

# Use a thread count of 2
(1..128).peach(2) { |i| sleep 1 } # Takes 64 seconds

pmap's People

Contributors

bruceadams avatar davidbiehl avatar shepmaster avatar p8952 avatar johana-star avatar zph avatar

Watchers

Brad Ediger avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.