How to get number of processors in linux with lua api?

In windows, I can get it with vim.env.NUMBER_OF_PROCESSORS.
But I can’t figure it out how to get it in unix system.
os.execute(“nproc”) returns a wrong value.

if fn.has("win32") > 0 then
    u.num_of_processers = vim.env.NUMBER_OF_PROCESSORS
elseif fn.has("unix") > 0 then
    u.num_of_processers = 4 -- os.execute("nproc") return wrong value
end

What does nproc give you run it in the terminal? And what does os.execute("nproc") give you? And which distro are you using?

os.execute('nproc') returns its status code, not output. You can use tonumber(vim.fn.system('nproc')) though.

Like zeertzjq said, it return 256 (the status code?).
But when I run lua in terminal and run this line, it returns 4 (my number of processors).

You could also use vim.loop.cpu_info() it returns a list of objects with model, speed, and times (idle, irq, nice, sys, user). To count the cores you can get the number of items in the list: #vim.loop.cpu_info().

Note that it will be counting with hyper threads, so actual physical core count might be half of whatever number is returned. But that’s the same for nproc as well

local uv = require('luv')

local cpus = uv.available_parallelism()

https://github.com/luvit/luv/blob/master/docs.md#uvavailable_parallelism