python multiprocessing arguments: deep copy?(python多处理参数:深拷贝?)
问题描述
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 上,这是通过写时复制"来提高效率的.来自 fork 手册页:
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多处理参数:深拷贝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:python多处理参数:深拷贝?
基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 筛选NumPy数组 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01