329 words, 2 min read

When optimizing code for performance or parallel processing, it's often useful to know how many CPU cores are available. Python provides several easy ways to retrieve this information, depending on whether you want the logical (including hyper-threaded) or physical cores.

The simplest and most portable way is by using os.cpu_count():

import os
cores = os.cpu_count()
print(f"Number of CPU cores: {cores}")

This returns the total number of logical cores β€” that is, physical cores multiplied by the number of threads per core. For example, a 4-core CPU with hyperthreading will typically report 8.

The multiprocessing module internally uses the same mechanism but provides a clearer intent when you’re working with parallel tasks:

import multiprocessing
cores = multiprocessing.cpu_count()
print(f"Number of CPU cores: {cores}")

This is often preferred when you’re spawning processes using multiprocessing.Pool, since it aligns semantically with process-based parallelism.

If you want to distinguish between physical and logical cores, use the psutil library (you may need to install it first using pip install psutil):

import psutil
logical = psutil.cpu_count(logical=True)
physical = psutil.cpu_count(logical=False)
print(f"Logical cores: {logical}")
print(f"Physical cores: {physical}")

This gives you a more accurate view of the hardware, especially on systems that support hyperthreading.

Knowing the number of cores helps you make better decisions when:

  • Configuring thread or process pools (e.g., in concurrent.futures, multiprocessing, or frameworks like joblib)
  • Tuning workloads in data processing pipelines
  • Running performance benchmarks that scale with CPU availability

Example: creating a process pool that scales automatically

from multiprocessing import Pool, cpu_count
def worker(n):
return n * n
if __name__ == "__main__":
with Pool(processes=cpu_count()) as pool:
results = pool.map(worker, range(10))
print(results)

This ensures your program uses all available cores without hardcoding the number.

Method Library Returns Notes
os.cpu_count() os Logical cores Built-in and cross-platform
multiprocessing.cpu_count() multiprocessing Logical cores Semantically clearer in parallel code
psutil.cpu_count(logical=False) psutil Physical cores Requires external dependency

When in doubt, use os.cpu_count() for a quick and portable solution β€” and switch to psutil if you need more detailed control over physical versus logical cores.