Home > Net >  How to get the number of available cores in python
How to get the number of available cores in python

Time:07-24

The standard method that I know to get number of cores in python is to use multiprocess.cpu_count

import multiprocessing
print(multiprocessing.cpu_count())

Also, when creating a task we can specify some cores that the process can access using taskset.

Apparently cpu_count is always getting the number of available cores of the machine, regardless if the process was restricted to use only some of those cores.

python nproc.py
taskset 7 python nproc.py
taskset 1 python nproc.py

nproc on the other hand gives the number of cores available for the process.

taskset 7 nproc # gives at most 3
taskset 1 nproc # gives 1

I know I could invoke nproc or taskset -p {os.getpid()} to get the correct number of cores. Is there another way to do this, without relying on other programs (reading /proc/{os.pid()}/* could be a solution).

Thank you

CodePudding user response:

According to the docs, this may be of limited availability, but it seems the os library has what you want;

Interface to the scheduler

These functions control how a process is allocated CPU time by the operating system. They are only available on some Unix platforms. For more detailed information, consult your Unix manpages.

New in version 3.3.

...

os.sched_getaffinity(pid)

Return the set of CPUs the process with PID pid (or the current process if zero) is restricted to.

tested using Ubuntu 20.04.2 (WSL):

aaron@DESKTOP:/mnt/c$ python3 -c "import os; print(os.sched_getaffinity(0))"
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
aaron@DESKTOP:/mnt/c$ taskset 7 python3 -c "import os; print(os.sched_getaffinity(0))"
{0, 1, 2}

Edit: on windows you'll probably have to look to psutil

  • Related