I create a process pool with four processes and use imap_unordered
to compute things in parallel and get intermediate results.
The code looks like this:
import multiprocessing
def f(x):
print(f"Start {x} on {multiprocessing.current_process().name}")
# do stuff
print(f"Finish {x} on {multiprocessing.current_process().name}")
def main():
pool = multiprocessing.Pool(processes=4)
iterable_in = [i for i in range(1000)] # input iterable
for result in pool.imap_unordered(f, iterable_in, chunksize=5):
# do stuff with result
pass
if __name__ == "__main__":
main()
The output looks as follows:
...
Finish 71 on ForkPoolWorker-4
Start 72 on ForkPoolWorker-4
Finish 76 on ForkPoolWorker-3
Start 77 on ForkPoolWorker-3
Finish 63 on ForkPoolWorker-1
Start 64 on ForkPoolWorker-1
Start 85 on ForkPoolWorker-5
Finish 73 on ForkPoolWorker-4
Start 74 on ForkPoolWorker-4
Finish 68 on ForkPoolWorker-2
Start 69 on ForkPoolWorker-2
Finish 85 on ForkPoolWorker-5
Start 86 on ForkPoolWorker-5
For the first iterations it looks fine, but at some point a fifth worker seems to be spawned continuing with a new chunk. From that point on, only workers 1, 2, 4 and 5 seems to proceed and worker 3 never finishes its current chunk. htop
shows a continuous load average of 4.00 and only four child processes.
Any ideas what could happened here?