python 多处理参数:深度复制?
from multiprocessing import Process
# c is a container
p = Process(target = f, args = (c,))
p.start()
我假设将 c
的深层副本传递给函数 f
因为浅层副本在新进程的情况下没有任何意义(新进程无权访问来自调用进程的数据)。
但这个深拷贝是如何定义的呢? 副本中有一整套一组注释 .deepcopy()
文档,所有这些注释也适用于此吗? multiprocessing
文档什么也没说......
from multiprocessing import Process
# c is a container
p = Process(target = f, args = (c,))
p.start()
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).
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您创建
Process
实例时,Python 在底层会发出一个fork()
。这将创建一个子进程,其内存空间是其父进程的精确副本——因此分叉时存在的所有内容都会被复制。在 Linux 上,这是通过“写时复制”来提高效率的。从 fork 手册页:
When you create a
Process
instance, under the hood Python issues afork()
. 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.On Linux this is made efficient through "copy-on-write". From the fork man page: