简体   繁体   English

当您在多处理目标 function 中传递它时,是否创建了一个新的 object?

[英]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.

相关问题 如何在多处理中将一个管理器变量和另一个变量传递给目标 function? - How do you pass a manager variable and another variable to target function in multiprocessing? 将函数或类实例传递给multiprocessing.Process的“ target”参数是否安全,而没有密集复制的风险? - Is it safe to pass a function or a class instance to the “target” argument of multiprocessing.Process without the risk of dense copying? 你能把 arguments 传递给 ZA7F5F35426B927411FC9231B5638271 中的 function object 吗? - Can you pass arguments to a function object in Python? 对象方法作为多处理过程目标 - Object method as multiprocessing.Process target Django - 创建新的 object 时更新 ForeignKey - Django - update ForeignKey when new object is created 将数据帧附加到多处理目标函数之外的列表 - Append dataframes to a list ouside of multiprocessing target function 运行时函数目标为multiprocessing.Process - runtime function target to multiprocessing.Process 从Jupyter Notebook中的multiprocessing.Process调用时,目标函数未分配给类属性 - Target function does not assign to class attribute when called by multiprocessing.Process from within Jupyter notebook 您如何正确地将字节对象传递给Python中的新线程? - How do you properly pass a bytes object to a new Thread in Python? Python-当`new`参数不是DEFAULT时,为什么模拟补丁装饰器不会将模拟对象传递给测试函数 - Python - why mock patch decorator does not pass the mocked object to the test function when `new` argument is not DEFAULT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM