Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ gem 'rake'
gem 'rspec'
gem 'activerecord'
gem 'ruby-progressbar'
gem 'rspec-rerun'
gem 'rspec-legacy_formatters'
gem 'rubocop'
gem 'rubocop-rake'
gem 'rubocop-rspec'
Expand Down
4 changes: 1 addition & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ require 'bundler/setup'
require 'bundler/gem_tasks'
require 'bump/tasks'
require "rspec/core/rake_task"
require 'rspec-rerun/tasks'

task default: ["spec", "rubocop"]

desc "Run tests"
task spec: "rspec-rerun:spec"
RSpec::Core::RakeTask.new(:spec)

desc "Run rubocop"
task :rubocop do
Expand Down
21 changes: 15 additions & 6 deletions lib/parallel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def kill_on_ctrl_c(pids, options)

if @to_be_killed.empty?
old_interrupt = trap_interrupt(signal) do
warn 'Parallel execution interrupted, exiting ...'
warn 'parallel: execution interrupted, exiting ...'
@to_be_killed.flatten.each { |pid| kill(pid) }
end
end
Expand Down Expand Up @@ -266,23 +266,24 @@ def map(source, options = {}, &block)
options = options.dup
options[:mutex] = Mutex.new

if options[:in_processes] && options[:in_threads]
raise ArgumentError, "Please specify only one of `in_processes` or `in_threads`."
if options.slice(:in_processes, :in_threads, :in_ractors).size > 1
raise ArgumentError, "Use only one of `in_processes`, `in_threads`, or `in_ractors`."
elsif RUBY_PLATFORM.include?('java') && !options[:in_processes]
method = :in_threads
size = options[method] || processor_count
elsif options[:in_threads]
method = :in_threads
size = options[method]
elsif options[:in_ractors]
raise ArgumentError, "in_ractors does not support blocks" if block
method = :in_ractors
size = options[method]
else
method = :in_processes
if Process.respond_to?(:fork)
size = options[method] || processor_count
else
warn "Process.fork is not supported by this Ruby"
warn "parallel: Process.fork is not supported by this Ruby"
size = 0
end
end
Expand All @@ -295,11 +296,12 @@ def map(source, options = {}, &block)

result =
if size == 0
block = ractor_block(options) if method == :in_ractors
work_direct(job_factory, options, &block)
elsif method == :in_threads
work_in_threads(job_factory, options.merge(count: size), &block)
elsif method == :in_ractors
work_in_ractors(job_factory, options.merge(count: size), &block)
work_in_ractors(job_factory, options.merge(count: size))
else
work_in_processes(job_factory, options.merge(count: size), &block)
end
Expand Down Expand Up @@ -379,7 +381,7 @@ def physical_processor_count_windows
end
if !result || $?.exitstatus != 0
# Bail out if both commands returned something unexpected
warn "guessing pyhsical processor count"
warn "parallel: guessing physical processor count"
processor_count
else
# powershell: "\nNumberOfCores\n-------------\n 4\n\n\n"
Expand Down Expand Up @@ -528,6 +530,13 @@ def work_in_ractors(job_factory, options)
exception || results
end

# ractors cannot execute blocks, so run the callback directly when not using ractors
def ractor_block(options)
klass, method_name = options[:ractor] ||
raise(ArgumentError, "pass the code you want to execute as `ractor: [ClassName, :method_name]`")
->(*args) { klass.send(method_name, *args) }
end

def ractor_build(use_port)
args = use_port ? [Ractor::Port.new] : []
ractor = Ractor.new(*args) do |port|
Expand Down
17 changes: 13 additions & 4 deletions spec/parallel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ def kill_process_with_name(file, signal = 'INT')
end

def execute_start_and_kill(command, amount, signal = 'INT')
file = 'spec/cases/parallel_start_and_kill.rb'
t = nil
lambda {
t = Thread.new { ruby("spec/cases/parallel_start_and_kill.rb #{command} 2>&1 && echo 'FINISHED'") }
t = Thread.new { ruby("#{file} #{command} 2>&1 && echo 'FINISHED'") }
sleep 1.5
kill_process_with_name('spec/cases/parallel_start_and_kill.rb', signal)
kill_process_with_name(file, signal)
sleep 1
}.should change { `ps`.split("\n").size }.by amount
# count only our own processes: sh wrapper, ruby, and forked workers all carry the file name
}.should change { `ps -f`.split("\n").count { |line| line.include?(file) } }.by amount
t.value
end

Expand Down Expand Up @@ -331,11 +333,18 @@ def cpus
it "can run with 0 by not using #{type}" do
Thread.should_not_receive(:exclusive)
Process.should_not_receive(:fork)
result = Parallel.map([1, 2, 3, 4, 5, 6, 7, 8, 9], "in_#{type}": 0) { |x| x + 2 }
result =
if type == "ractors"
callback = Class.new { def self.call(x) = x + 2 }
Parallel.map([1, 2, 3, 4, 5, 6, 7, 8, 9], in_ractors: 0, ractor: [callback, :call])
else
Parallel.map([1, 2, 3, 4, 5, 6, 7, 8, 9], "in_#{type}": 0) { |x| x + 2 }
end
result.should == [3, 4, 5, 6, 7, 8, 9, 10, 11]
end

it "does not dump/load when running with 0 using #{type}" do
skip if type == "ractors" # blocks are not supported
p = -> { 1 }
result = Parallel.map([1, 2], "in_#{type}": 0) { p }
result.first.call.should == 1
Expand Down