Skip to content

Commit 21fdad7

Browse files
authored
Merge pull request #348 from y-yagi/use_available_processor_count
Use cgroups aware processor count by default
2 parents 9644d13 + 39f5630 commit 21fdad7

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ Tips
196196
- [Isolation] Do not reuse previous worker processes: `isolation: true`
197197
- [Stop all processes with an alternate interrupt signal] `'INT'` (from `ctrl+c`) is caught by default. Catch `'TERM'` (from `kill`) with `interrupt_signal: 'TERM'`
198198
- [Process count via ENV] `PARALLEL_PROCESSOR_COUNT=16` will use `16` instead of the number of processors detected. This is used to reconfigure a tool using `parallel` without inserting custom logic.
199+
- [Process count] `parallel` uses a number of processors seen by the OS for process count by default. If you want to use a value considering CPU quota, please add `concurrent-ruby` to your `Gemfile`.
199200

200201
TODO
201202
====

lib/parallel.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,10 @@ def physical_processor_count
337337
end
338338
end
339339

340-
# Number of processors seen by the OS, used for process scheduling
340+
# Number of processors seen by the OS or value considering CPU quota if the process is inside a cgroup,
341+
# used for process scheduling
341342
def processor_count
342-
require 'etc'
343-
@processor_count ||= Integer(ENV['PARALLEL_PROCESSOR_COUNT'] || Etc.nprocessors)
343+
@processor_count ||= Integer(ENV['PARALLEL_PROCESSOR_COUNT'] || available_processor_count)
344344
end
345345

346346
def worker_number
@@ -695,5 +695,13 @@ def instrument_start(item, index, options)
695695
return unless (on_start = options[:start])
696696
options[:mutex].synchronize { on_start.call(item, index) }
697697
end
698+
699+
def available_processor_count
700+
require 'concurrent-ruby'
701+
Concurrent.available_processor_count.floor
702+
rescue LoadError, NoMethodError
703+
require 'etc'
704+
Etc.nprocessors
705+
end
698706
end
699707
end

spec/parallel_spec.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ def without_ractor_warning(out)
4949
(1..999).should include(Parallel.processor_count)
5050
end
5151
end
52-
53-
it 'uses Etc.nprocessors in Ruby 2.2+' do
54-
defined?(Etc).should == "constant"
55-
Etc.respond_to?(:nprocessors).should == true
56-
end
5752
end
5853

5954
describe ".physical_processor_count" do

0 commit comments

Comments
 (0)