Skip to content

Commit 393ad72

Browse files
Earlopaineregon
authored andcommitted
Remove dependency on win32ole
This will become bundled in Ruby 3.5 Unfortunately there is no portable way of checking for this. The wmic command is deprecated, though I don't observe this myself on W11 (yet?)
1 parent 22f337c commit 393ad72

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

lib/concurrent-ruby/concurrent/utility/processor_counter.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,20 @@ def compute_physical_processor_count
6868
end
6969
cores.count
7070
when /mswin|mingw/
71-
require 'win32ole'
72-
result_set = WIN32OLE.connect("winmgmts://").ExecQuery(
73-
"select NumberOfCores from Win32_Processor")
74-
result_set.to_enum.collect(&:NumberOfCores).reduce(:+)
71+
# Get-CimInstance introduced in PowerShell 3 or earlier: https://learn.microsoft.com/en-us/previous-versions/powershell/module/cimcmdlets/get-ciminstance?view=powershell-3.0
72+
result = run('powershell -command "Get-CimInstance -ClassName Win32_Processor | Select-Object -Property NumberOfCores"')
73+
if !result || $?.exitstatus != 0
74+
# fallback to deprecated wmic for older systems
75+
result = run("wmic cpu get NumberOfCores")
76+
end
77+
if !result || $?.exitstatus != 0
78+
# Bail out if both commands returned something unexpected
79+
processor_count
80+
else
81+
# powershell: "\nNumberOfCores\n-------------\n 4\n\n\n"
82+
# wmic: "NumberOfCores \n\n4 \n\n\n\n"
83+
result.scan(/\d+/).map(&:to_i).reduce(:+)
84+
end
7585
else
7686
processor_count
7787
end
@@ -81,6 +91,11 @@ def compute_physical_processor_count
8191
return 1
8292
end
8393

94+
def run(command)
95+
IO.popen(command, &:read)
96+
rescue Errno::ENOENT
97+
end
98+
8499
def compute_cpu_quota
85100
if RbConfig::CONFIG["target_os"].include?("linux")
86101
if File.exist?("/sys/fs/cgroup/cpu.max")

0 commit comments

Comments
 (0)