Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiprocessing.cpu_count returning wrong number of cores

Tags:

python

I try to use the following code to decide the number of cores of my laptop:

import multiprocessing
multiprocessing.cpu_count()

The result is 8, but when I open the system report, I can see that I have only 1 CPU with 4 cores.

What's wrong with cpu_count()?

like image 632
satoru Avatar asked Jul 05 '16 03:07

satoru


People also ask

What is Multiprocessing Cpu_count ()?

help(multiprocessing) One of the useful functions in multiprocessing is cpu_count() . This returns the number of CPUs (computer cores) available on your computer to be used for a parallel program. Type into ipython. print(multiprocessing.cpu_count())

How do I print the number of cores in Python?

We can also use the os. cpu_count() function to return the count of the number of logical CPU cores in the current system.

Does multiprocessing use multiple CPUs?

Multiprocessing: The use of two or more CPUs within a single computer system [4][5]. The term also refers to the ability of a system to support more than one processor or the ability to allocate tasks between them.

How many CPU does Python use?

In Python, single-CPU use is caused by the global interpreter lock (GIL), which allows only one thread to carry the Python interpreter at any given time. The GIL was implemented to handle a memory management issue, but as a result, Python is limited to using a single processor.


2 Answers

You have 4 physical cores, but 8 logical processors. For example, you may have a processor with hyper-threading.

From SuperUser: Difference Between Cores and Processors

like image 200
Lack Avatar answered Oct 13 '22 01:10

Lack


What CPU do you have, it might be counting the Physical cores + virtual cores.

like image 29
Ricardo Oyarzun Avatar answered Oct 13 '22 00:10

Ricardo Oyarzun