Python Glossary Русский

Python 3.4+osfunction

os.cpu_count

os — system

Return the number of logical CPUs (or None if undetermined).

Syntax

os.cpu_count()

It counts the system's processors, not those available to the program: in a limited container or with CPU affinity fewer may actually be usable. The number usable by the current process comes from os.process_cpu_count (3.13+) or len(os.sched_getaffinity(0)) on Linux. It may return None — for a pool size write os.cpu_count() or 1.

Examples

import os
print(os.cpu_count())  # → ? число логических процессоров, например 8 (или None, если определить не удалось)
cpus = os.cpu_count() or 1  # страховка: None заменяем на 1
print(min(4, cpus))  # → ? размер пула воркеров: 4 на машине с 4+ ядрами
print(len(os.sched_getaffinity(0)))  # → ? только Unix: сколько ядер реально доступно процессу (может быть меньше os.cpu_count())

See also

Open in the full glossarydocs.python.orgReport an inaccuracy