Skip to content

Commit

Permalink
Remove dependency on win32ole
Browse files Browse the repository at this point in the history
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?)
  • Loading branch information
Earlopain committed Jun 7, 2024
1 parent f66f105 commit 7ee51bd
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions lib/parallel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,7 @@ def physical_processor_count
end
cores.count
when /mswin|mingw/
require 'win32ole'
result_set = WIN32OLE.connect("winmgmts://").ExecQuery(
"select NumberOfCores from Win32_Processor"
)
result_set.to_enum.collect(&:NumberOfCores).reduce(:+)
physical_processor_count_windows
else
processor_count
end
Expand All @@ -358,6 +354,33 @@ def worker_number=(worker_num)

private

def physical_processor_count_windows
# 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
result = run(
'powershell -command "Get-CimInstance -ClassName Win32_Processor ' \
'| Select-Object -Property NumberOfCores"'
)
if !result || $?.exitstatus != 0
# fallback to deprecated wmic for older systems
result = run("wmic cpu get NumberOfCores")
end
if !result || $?.exitstatus != 0
# Bail out if both commands returned something unexpected
warn "guessing pyhsical processor count"
processor_count
else
# powershell: "\nNumberOfCores\n-------------\n 4\n\n\n"
# wmic: "NumberOfCores \n\n4 \n\n\n\n"
result.scan(/\d+/).map(&:to_i).reduce(:+)
end
end

def run(command)
IO.popen(command, &:read)
rescue Errno::ENOENT
# Ignore
end

def add_progress_bar!(job_factory, options)
if (progress_options = options[:progress])
raise "Progressbar can only be used with array like items" if job_factory.size == Float::INFINITY
Expand Down

0 comments on commit 7ee51bd

Please sign in to comment.