Python Glossary Русский

all Python 3multiprocessingfunction

multiprocessing.cpu_count()

processes

The number of logical CPUs in the system. A common starting point for a pool size, but it is not the number of CPUs available to your particular process.

Syntax

multiprocessing.cpu_count()

cpu_count() ignores affinity, container and cgroup limits: in Docker capped at 2 cores it still reports every host core, and a pool of that size only thrashes. For the CPUs actually usable there is os.process_cpu_count() (3.13+), which Pool uses by default since 3.13. Unlike os.cpu_count(), which returns None, multiprocessing.cpu_count() raises NotImplementedError when the count is unknown. Since 3.13 the result can be overridden with -X cpu_count.

Examples

import multiprocessing as mp
import os
if __name__ == '__main__':
    print(mp.cpu_count())  # → ? число логических CPU машины
    print(mp.cpu_count() == os.cpu_count())  # → True
# Python 3.13+
import multiprocessing as mp
import os
if __name__ == '__main__':
    print(os.process_cpu_count() <= mp.cpu_count())  # → True — доступных не больше, чем есть

See also

Open in the full glossarydocs.python.orgReport an inaccuracy