I wrote the following code, which doesn't seem to distribute tasks in the way I would expect:
import multiprocessing
from time import sleep
import os
def multi_test(time):
print(f'{os.getpid()}: {time}s')
sleep(time)
pool = multiprocessing.Pool(2)
pool.map(multi_test, [1,1,1,1,4,1,1,1,1])
pool.close()
pool.join()
The output shows the processor ID and time:
68858: 1s
68857: 1s
68858: 1s
68857: 1s
68857: 4s
68858: 1s
68858: 1s
68858: 1s # 2 second delay after this line, because the other core is doing the next task (WHY?)
68857: 1s
The code takes 7 seconds to run. I would have expected it to take 6 seconds, because the last four 1s tasks could be done at the same time as one 4s task, but python has given the last task to the same processor doing the 4s task.
<1><2><3><4><5><6><7>
68858: 1s 1s 1s 1s 1s
68857: 1s 1s <----4s---> 1s
Why is this happening and what can I do to change it?
If I take away two 1s tasks from the beginning that happen in parallel (i.e., [1,1,4,1,1,1,1]), then the result is as I would expect:
<1><2><3><4><5>
68858: 1s 1s 1s 1s 1s
68857: 1s <----4s--->