Skip to content

Commit d0e8d39

Browse files
committed
Use cgroups aware processor count by default
In containerized environments, a number of CPU cores isn't the same as the available CPUs. In this case, we need to consider cgroups. `concurrent-ruby` now has the method to get that since v1.3.1. I think it's better to use this for setting more container environment friendly default value. Ref: ruby-concurrency/concurrent-ruby#1038
1 parent 9644d13 commit d0e8d39

File tree

3 files changed

+4
-8
lines changed

3 files changed

+4
-8
lines changed

lib/parallel.rb

Lines changed: 3 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, used for process scheduling
341341
def processor_count
342-
require 'etc'
343-
@processor_count ||= Integer(ENV['PARALLEL_PROCESSOR_COUNT'] || Etc.nprocessors)
342+
require 'concurrent-ruby'
343+
@processor_count ||= Integer(ENV['PARALLEL_PROCESSOR_COUNT'] || Concurrent.available_processor_count.to_i)
344344
end
345345

346346
def worker_number

parallel.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ Gem::Specification.new name, Parallel::VERSION do |s|
1616
}
1717
s.files = `git ls-files lib MIT-LICENSE.txt`.split("\n")
1818
s.license = "MIT"
19+
s.add_runtime_dependency "concurrent-ruby", "~> 1.0", ">= 1.3.1"
1920
s.required_ruby_version = '>= 2.7'
2021
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)