I have a init method which initializes various primitive and complex data types and objects. In each process spawned by multiprocessing.Process, I'm printing a variable from init() method and an address of an initialized object.
I get different instances of the variable but the address of object remains the same. So, want to know what exactly happens to members of parent class during multiprocessing.Process call?
self.count = 0
self.db = pymongo.MongoClient()
for i in range(4):
p = multiprocessing.Process(target = self.consumer, args = (i,))
def consumer(self, i):
self.count += 1
Generally on Linux when a new process is created, a copy of the parent is generated.
At the beginning the two processes will be in the same state but with different address spaces.
To save time, Linux shares the memory of the parent with the child until both do not modify it. This is usually referred as Copy On Write.
Usually, due to their simplicity Pipes and Queues are the recommended ones.
The reason you see the same id is explained in the following question. As the new process has the same memory layout of the parent, in CPython the id will be the same.