Python Multiprocessing Behavior Of Pool / Starmap
Solution 1:
Which means that if some processes have cleared the Queue before others, they do not get new tasks to perform.
Is this correct?
No. The main purpose of multiprocess.Pool()
is to spread the passed workload to the pool of its workers - that's why it comes with all those mapping options - the only difference between its various methods is on how the workload is actually distributed and how the resulting returns are collected.
In your case, the iterable you're generating with [(s, t0, tf, folder) for s in signals]
will have each of its elements (which ultimately depends on the signals
size) sent to the next free worker (invoked as compute_solutions(s, t0, tf, folder)
) in the pool, one at a time (or more if chunksize
parameter is passed), until the whole iterable is exhausted. You do not get to control which worker executes which part, tho.
The workload may also not be evenly spread - one worker may process more entries than another in dependence of resource usage, execution speed, various internal events...
However, using map
, imap
and starmap
methods of multiprocessing.Pool
you get the illusion of even and orderly spread as they internally synchronize the returns from each of the workers to match the source iterable (i.e. the first element of the result will contain the resulting return from the called function with the first element of the iterable). You can try the async/unordered versions of these methods if you want to see what actually happens underneath.
Therefore, you get the smarter system by default, but you can always use multiprocessing.Pool.apply_async()
if you want a full control over your pool of workers.
As a side note, if you're looking on optimizing the access to your iterable itself (as the pool map options will consume a large part of it) you can check this answer.
Finally,
What are the differences between them and when should we use one or the other?
Instead of me quoting here, head over to the official docs as there is quite a good explanation of a difference between those.
Post a Comment for "Python Multiprocessing Behavior Of Pool / Starmap"