Unexpected Behavior With Multiprocessing Pool
In the code below, I was expecting the output to be 2 as I'm changing the value of config before assigning function to pool for multiprocessing, but instead I'm getting 5. I'm sure
Solution 1:
The new processes are created when you create the pool. After that, changes made to the parent memory space, except for stuff that is passed in a pool function like .map
, are not seen by the subprocess. Forking systems like linux create copy-on-write views to the parent memory space - and that write results in a unique memory block for the parent, not seen by the subprocess. Spawning systems reimport modules (setting global variables) and then try to pickle/unpickle state for the subprocesses. In both cases, this is completed before the Pool
class initialization returns to your program.
Post a Comment for "Unexpected Behavior With Multiprocessing Pool"