I'm new to multiprocessing and I'm trying to understand it. I noticed that whenever you pass in an object into args, it seems like a duplicate of it is made?
Example:
Let's say I have this z1.py file with
import multiprocessing
from z2 import fun2
def fun():
q = multiprocessing.Queue()
print('in fun', id(q))
p = multiprocessing.Process(target=fun2, args=(q,))
p.start()
if __name__ == '__main__':
fun()
and a z2.py file
def fun2(q):
print('in fun2', id(q))
I was expecting to see the same object id in both print statements, but instead I get this:
in fun 2311052901680
in fun2 1990946259440
Can anyone help me understand why the object doesn't have the same object ID if you're passing in the same object you created into p?
What is id ?
it is the address where the object lies in the process virtual memory address space.
When you create another process it will have a separate different part of memory, sending one object from one process to another, python has to pickle the object then pass it to the other process then unpickle the object in the other process,
it shouldn't end up in the same virtual address as the first object because it is not the same object anymore and it's not the same address space anymore, it's a complete new object that was created in the second process, that just "looks like" your original object.
So yes, it's a new object.