Search code examples
pythonpython-3.xmultiprocessingpython-multiprocessing

Why does Python's multiprocess use all cores if I am telling it not to?


I am running a script that includes

import multiprocess as mp

def sample():
   # ... do things...
   return x

num_cores = 50
num_samples = 1000
with mp.Pool(num_cores) as p:
    samples = p.starmap(self, [() for i in range(num_samples)])

There are 128 cores in my server. However, even though I create a pool with just 50 of them, when I run htop in the terminal I see that all 128 cores are being used by my script, and each at 100% usage.

Why is this happening? I thought only num_cores cores could be used my script. Or does this depend on what sample() is actually doing?


Solution

  • The argument to mp.Pool tells it how many processes, at most, to run at a time; it does not constrain how many cores those processes use.

    If each process uses more than one core (whether by multithreading, or starting more subprocesses of its own, or otherwise), a multiprocessing pool can easily use more cores than the number of concurrent processes it's told to invoke.