On this page
os.cpu_count
Prev: os.close | Up: os | Next: os.DirEntry
Rel: os.abort, os.access, os.chdir, os.chmod
Ref: docs.python.org — os.cpu_count
Tags: #public #python3 #stdlib #os
Import
import os
from os import cpu_count
Signature & Overview
os.cpu_count()
Return the number of logical CPUs in the system.
Usage
import os
cores = os.cpu_count() or 1
assert cores >= 1
Gotchas
- Can return
None: Always fall back (os.cpu_count() or 1). - Ignores cgroup/container quotas: In Docker/Kubernetes,
os.process_cpu_count()(Python 3.13+) orlen(os.sched_getaffinity(0))(Linux) reflects usable CPUs more accurately.
REPL Inspect
from os import cpu_count
help(cpu_count)
Linked from
Related notes
- tuts/python3/stdlib_module/os/close Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/DirEntry Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/os/abort Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/access Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chdir Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chmod Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chown shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/dup shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/dup2 shared tags: #os, #stdlib