python multiprocessing参数:深层复制? [英] python multiprocessing arguments: deep copy?

查看:327
本文介绍了python multiprocessing参数:深层复制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from multiprocessing import Process
# c is a container
p = Process(target = f, args = (c,))
p.start()

我假定将c的深层副本传递给函数f,因为在新进程的情况下,浅表副本是没有意义的(新进程无法访问调用进程中的数据)

I assume a deep copy of c is passed to function f because shallow copy would make no sense in the case of a new process (the new process doesn't have access to the data from the calling process).

但是该深层副本如何定义? copy.deepcopy()文档中有一整套注释集 ,所有这些说明在这里也适用吗? multiprocessing文档什么也没说...

But how is this deep copy defined? There is a whole set of notes in the copy.deepcopy() documentation, do all these notes apply here as well? The multiprocessing documentation says nothing...

推荐答案

创建Process实例时,Python会在fork()下发出fork().这将创建一个子进程,该子进程的内存空间是其父进程的精确副本-因此,将复制分支时存在的所有内容.

When you create a Process instance, under the hood Python issues a fork(). This creates a child process whose memory space is an exact copy of its parent -- so everything existing at the time of the fork is copied.

在Linux上,可通过写时复制"来提高效率.从叉手册页:

On Linux this is made efficient through "copy-on-write". From the fork man page:

fork()创建一个子进程,该子进程 仅与父进程不同 在其PID和PPID中,实际上 资源利用率设置为 0.文件锁和挂起的信号不会被继承.

fork() creates a child process that differs from the parent process only in its PID and PPID, and in the fact that resource utilizations are set to 0. File locks and pending signals are not inherited.

在Linux下,实现fork() 使用写时复制页面,因此唯一 它招致的惩罚是时间和 复制所需的内存 父级的页表,并创建一个 儿童的独特任务结构.

Under Linux, fork() is implemented using copy-on-write pages, so the only penalty that it incurs is the time and memory required to duplicate the parent's page tables, and to create a unique task structure for the child.

这篇关于python multiprocessing参数:深层复制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆