Search code examples
pythonmultiprocessingoperating-systemcpu-cores

Get Core Count in Python (NOT THREAD COUNT)


I was trying to find a way to get the number of CPU cores somebody has using Python. Almost all the answers I found were either:

multiprocessing.cpu_count()

or

os.cpu_count()

While these options are great for seeing how many threads the computer has, it doesn't tell me how many cores it has.


Solution

  • If you don't mind using a third party library, using psutil will do the trick:

    import psutil
    
    print(f'Number of physical cores: {psutil.cpu_count(logical=False)}')