System: Debian 10 running in the Google Cloud Platform as a DLVM, n1-highmem-16 machine type.
I'm doing some heavy calculations in Python, and I'm using the multiprocessing
module to use a lot more of my CPUs. I've noticed, while watching my calculations spin in htop, that the idle processors jump around fairly frequently. That is, sometimes CPU 0 will be idle, and then it will become busy. Sometime later it might become idle again. This leads me to believe that the active jobs are probably jumping around as well. Surely the constant CPU hopping is slowing down the calculations? Is there a way to prevent the constant CPU hopping? Or is that a bad idea?
Here is a MWE of Python code you can run that will exercise multiprocessing
long enough to detect the kind of hopping I'm talking about:
import multiprocessing
import numpy as np
import time
def function_calculation():
data = np.zeros(1000000)
t1 = time.perf_counter()
direction = 1
while time.perf_counter() - t1 < 600: # 10 minutes.
data = data + direction
direction = direction * (-1)
num_processors = multiprocessing.cpu_count() - 1 # leave one out for system stuff.
for proc in range(num_processors):
p = multiprocessing.Process(target=function_calculation)
p.start()