parallelize

Simple multi-threading for Ruby.

Installation

gem install parallelize

Examples

require 'rubygems'
require 'parallelize'

parallelize(4) do
  puts "I'm a thread"

  # ...
end

# Zero-based thread index
parallelize(4) do |thread_idx|
  puts "I'm thread ##{thread_idx}"

  # ...
end

# Enumerable#peach
(0..100).peach(4) do |elem, thread_idx|
  puts "Thread ##{thread_idx} processing #{elem}"
end

# Enumerable#pmap
(0..100).pmap(4) do |elem|
  elem ** 2
end

(0..100).pmap(8) do |elem, thread_idx|
  elem * thread_idx
end

Collecting exceptions

begin
  parallelize(4, true) do |elem, thread_idx|
    # Each thread can complete its block even when some other threads throw exceptions
  end
rescue ParallelException => e
  p e.exceptions
end

Contributing to parallelize

Copyright © 2011 Junegunn Choi. See LICENSE.txt for further details.