I already found a solution for "Most unixes" via cmdline "cat /proc/cpuinfo", but a pure-ruby solution would be nicer...
+5
A:
Surely if you can cat
it, you can open, read and close it using the standard features of the language without resorting to a system()
-type call.
You may just need to detect what platform you're on dynamically and either:
- use the
/proc/cpuinfo
"file" for Linux; or - communicate with WMI for Windows.
That last line can use:
require 'win32ole'
wmi = WIN32OLE.connect("winmgmts://")
info = wmi.ExecQuery ("select * from Win32_ComputerSystem")
Then use info's NumberOfProcessors item.
paxdiablo
2009-05-21 05:46:02
Or possibly NumberOfProcessors. ;-)
Stobor
2009-05-21 06:00:51
Thanks, I hoped it would be something easy, for now I will skip this feature :)
grosser
2009-05-21 06:08:53
Thanks, @Stobor, fixed.
paxdiablo
2009-05-21 06:10:52
+5
A:
with JRuby you can check it with the following Java code:
Runtime runtime = Runtime.getRuntime();
int numberOfProcessors = runtime.availableProcessors();
dfa
2009-05-21 06:39:07
I've voted this one up particularly because of the implicit point that with JRuby you can actually use those extra cores with Ruby's Thread class! Might it be worth adding this version of the example too?#!/usr/bin/jrubyinclude Javaputs "You have #{java.lang.Runtime.getRuntime.availableProcessors} cores"
Mark Longair
2010-06-10 14:08:34
+6
A:
$ gem install facter
$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'facter'
=> true
irb(main):003:0> Facter.loadfacts
=> true
irb(main):004:0> puts Facter.sp_number_processors
2
=> nil
irb(main):005:0>
This facter gem is the best, it's not platform specific and designed to do this exact thing.
Anko
2009-05-21 08:23:30
nice solution, ill check it out, since i am working on a public library i did not want to add an extra dependency for something this trivial
grosser
2009-05-27 05:09:53