Detect number of CPUs installed

前端 未结 12 945
孤城傲影
孤城傲影 2020-12-24 05:29

I already found a solution for \"Most unixes\" via cat /proc/cpuinfo, but a pure-Ruby solution would be nicer.

12条回答
  •  情书的邮戳
    2020-12-24 06:14

    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.

提交回复
热议问题