Python 使用多处理器内存

我写了一个可以概述如下所示的程序:


def loadHugeData//:
#load it
return data

def processHugeData/data, res_queue/:
for item in data:
#process it
res_queue.put/result/
res_queue.put/"END"/

def writeOutput/outFile, res_queue/:
with open/outFile, 'w'/ as f
res=res_queue.get//
while res!='END':
f.write/res/
res=res_queue.get//

res_queue = multiprocessing.Queue//

if __name__ == '__main__':
data=loadHugeData//
p = multiprocessing.Process/target=writeOutput, args=/outFile, res_queue//
p.start//
processHugeData/data, res_queue/
p.join//


真正的代码 /特别的
writeOutput//

/ 更困难。
writeOutput//

仅使用他作为参数的值 /也就是说,他没有提到
data

/

基本上,它将一系列大量数据加载到内存中并处理它。 输出记录委派给子过程 /实际上,它将数据写入多个文件,需要花费大量时间。/.
因此,每次处理数据的一个元素时,都会通过它发送到子处理 res_queue, 反过来,这会根据需要将结果写入文件。

子处理不需要获得访问,读取或更改加载的数据
loadHugeData//

. 必须仅使用子过程,只要主要过程发送给他
res_queue

. 它给我带来了我的问题和问题。

在我看来,子进程在巨大数据集的副本上接收它。 /检查使用内存时
top

/. 这是真的? 如果是这样,那么我怎样才能避免 id /基本上使用双内存/?

我用 Python 2.6, 该计划适用于 linux.
已邀请:

三叔

赞同来自:

模块
multiprocessing

实际上基于系统调用
fork

, 它创建了当前进程的副本。 自从您之前下载巨额数据以来
fork

/或者创造
multiprocessing.Process

/, 子进程继承了数据的副本。

但是,如果您工作的操作系统,实现 COW /copy-on-write/, 然后在物理内存中,如果您不更改父流或子进程中的数据,则只有一个数据副本 /父母和子公司都将使用相同的物理内存页面,但在不同的虚拟地址空间中/; 即便如此,即使是额外的内存也会被分配用于更改 /在步骤中
pagesize

/.

您可以通过致电避免这种情况
multiprocessing.Process

在下载大量数据之前。 然后将数据加载到父级时,不会在子进程中反映其他内存分配。

要回复问题请先登录注册