Ruby parallel gem use cases.

########################
# run with block
Parallel.in_processes do
  "Hello"
end
Parallel.in_threads do
  "Hello"
end
########################


########################
# run with count specified with ENV variable
# PARALLEL_PROCESSOR_COUNT=10 ruby test.rb
# test.rb
# Parallel.in_processes do
#  "Hello"
# end
#
########################

########################
# run with specified count
Parallel.in_processes(5) do
  "Hello"
end
Parallel.in_threads(5) do
  "Hello"
end
########################

########################
# map to run enumerables
Parallel.map(1..100) do |x|
  x
end
Parallel.map([1, 2, 3, 4, 5], in_processes: 4) do |x|
  x**2
end
Parallel.map([1, 2, 3, 4, 5], in_threads: 4) do |x|
  x**2
end
########################

########################
# map_with_index
Parallel.map_with_index(['a', 'b']) do |x, i|
  "#{x}#{i}"
end
Parallel.map_with_index(['a', 'b'], in_processes: 2) do |x, i|
  "#{x}#{i}"
end
Parallel.map_with_index(['a', 'b'], in_threads: 2) do |x, i|
  "#{x}#{i}"
end
########################

########################
# any?
Parallel.any?([nil, nil, 1], in_processes: 2) do |x|
  x
end # true
Parallel.any?([nil, nil, 1], in_threads: 2) do |x|
  x
end # true
########################

########################
# all?
Parallel.all?([true, nil, 1], in_processes: 2) do |x|
  x
end # false
Parallel.all?([true, true, true], in_threads: 2) do |x|
  x
end # true
########################

########################
# each - works like a map
Parallel.each(['a', 'b', 'c']) do |x|
  x
end
########################

########################
# each_with_index
Parallel.each_with_index(['a','b'], in_processes: 2) do |x, i|
  "#{x}#{i}"
end
Parallel.each_with_index(['a','b'], in_threads: 2) do |x, i|
  "#{x}#{i}"
end
########################

########################
# with progress bar
title = (ENV["TITLE"] == "true" ? true : "Doing stuff")
Parallel.map(1..50, :progress => title) do
  sleep 1 if $stdout.tty? # for debugging
end
########################

########################
# with_lambda
type = case ARGV[0]
when "PROCESSES" then :in_processes
when "THREADS" then :in_threads
else
  raise "Use PROCESSES or THREADS"
end

all = [3,2,1]
produce = lambda { all.pop || Parallel::Stop }
puts Parallel.map(produce, type => 2) { |(i, id)| "ITEM-#{i}" }
########################

########################
# with_queue
type = case ARGV[0]
when "PROCESSES" then :in_processes
when "THREADS" then :in_threads
else
  raise "Use PROCESSES or THREADS"
end

queue = Queue.new
Thread.new do
  sleep 0.2
  queue.push 1
  queue.push 2
  queue.push 3
  queue.push Parallel::Stop
end
puts Parallel.map(queue, type => 2) { |(i, id)| "ITEM-#{i}" }
########################

See reference here.

Post tagged with: ruby