If I call apply_async 10,000 times, assuming the OOM-killer doesn't interfere, will multiprocessing start them all simultaneously, or will it start them in batches. For example.. Every 100 starts, waiting for 90 to finish starting before starting any more?
Dustin
apply_async()is a method ofmultiprocessing.Poolobjects, and delivers all work to the number of processes you specified when you created thePool. Only that many tasks can run simultaneously. The rest are saved in queues (or pipes) by the multiprocessing machinery, and automatically doled out to processes as they complete tasks already assigned. Much the same is true of all thePoolmethods to which you feed multiple work items.A little more clarification:
apply_asyncdoesn't create, or start, any processes. The processes were created when you calledPool(). The processes just sit there and wait until you invokePoolmethods (likeapply_async()) that ask for some real work to be done.Example
Play with this:
Output is like:
You can answer most of your questions just by observing its behavior. The work items are queued up quickly. By the time we see "closing pool", all the work items have been queued, but 1478 have already completed, and about 98000 are still waiting for some process to work on them.
If you take the
sleep(0.01)out off(), it's much less revealing, because results come back almost as fast as work items are queued.Memory use remains trivial no matter how you run it, though. The work items here (the name of the function (
"f") and its pickled integer argument) are tiny.