[英]Is a new object created when you pass it in multiprocessing target function?
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?
我注意到,每当您将 object 传递给 args 时,似乎都会复制它?
Example:例子:
Let's say I have this z1.py file with假设我有这个 z1.py 文件
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和一个 z2.py 文件
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:我期待在两个打印语句中看到相同的 object id,但我得到的是:
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?任何人都可以帮助我理解为什么 object 没有相同的 object ID 如果您将创建的相同 object 传递到 p?
What is id?什么是身份证?
it is the address where the object lies in the process virtual memory address space.它是 object 在进程虚拟 memory 地址空间中的地址。
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,当您创建另一个进程时,它将有一个单独的不同部分 memory,将一个 object 从一个进程发送到另一个进程,python 必须对 object 进行 pickle,然后将其传递给另一个进程,然后在另一个进程中解开 object,
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.它不应该在与第一个 object 相同的虚拟地址结束,因为它不再是相同的 object并且它不再是相同的地址空间,它是在第二个进程中创建的全新的 object,只是“看起来像“你原来的 object。
So yes, it's a new object.所以是的,这是一个新的 object。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.