I am trying to understand why the following python script uses all of my CPU cores (8 core Ubuntu machine) when only 4 processes are requested
import os
import time
from concurrent import futures
def Sleeping(second):
pid = os.getpid()
print(f"Sleeping for {second} seconds (PID:{pid}).")
time.sleep(second)
seconds = [15, 15, 15, 15]
with futures.ProcessPoolExecutor() as executor:
for second in seconds:
executor.submit(Sleeping, second)
Let's say the main script PID is 120. Then four PID (121, 122, 123, 124) appear on my system monitor for 15 seconds sleeping functions. However, extra PID (125, 126, 127, 128) also appear when it is obvious that they do nothing. It seems like a waste of resources. I do not have this issue when I am using multiprocessing.Process instead. Any insight on what is going on with concurrent.futures as opposed to multiprocessing is also appreciated.