To my understanding, multiprocessing uses fork on Linux, which means each process created by multiprocessing has its own memory space and any changes made within do not affect other forked processes.
But I encountered this rather strange situation:
import multiprocessing
i = -1
def change(j):
global i
print(i, end=" ") # should print -1
i = j
with multiprocessing.Pool(20) as p:
p.map(change, range(20))
print(i) # should print -1
I thought this program would print exactly 21 -1, as multiprocessing creates 20 separate subprocesses whose memory spaces are not shared, which means the line i = j will not affect the value of i in any other processes; hence i = -1 at the time of printing.
However, the program actually printed a mix of -1 and a random amount of numbers between 0 and 19.
Example:
-1 -1 -1 -1 -1 4 -1 5 -1 6 -1 8 -1 -1 14 -1 -1 12 -1 -1 -1
So my question is, why did I not get exactly 21 -1?
Pool(20), it won't really create 20 processes until it is needed. Which means some tasks may be executed in the same process. You can printoutos.getpid()to have a check.