get system cpu count in python

To get the number of CPUs on a system in Python, you can use the multiprocessing or os module. Below are two ways to achieve this:

Using the multiprocessing module:

main.py
import multiprocessing

cpu_count = multiprocessing.cpu_count()
print("CPU count:", cpu_count)
95 chars
5 lines

Using the os module:

main.py
import os

cpu_count = os.cpu_count()
print("CPU count:", cpu_count)
69 chars
5 lines

Both methods return the number of available CPUs on the system. This information can be useful in multiprocessing programs, or for hardware monitoring applications.

gistlibby LogSnag