是否将共享只读数据复制到不同进程以进行多处理?

san*_*san 52 python numpy multiprocessing

我看到的那段代码看起来像这样:

glbl_array = # a 3 Gb array

def my_func( args, def_param = glbl_array):
    #do stuff on args and def_param

if __name__ == '__main__':
  pool = Pool(processes=4)
  pool.map(my_func, range(1000))
Run Code Online (Sandbox Code Playgroud)

有没有办法确保(或鼓励)不同的进程没有获得glbl_array的副本但共享它.如果没有办法停止复制,我将使用memmapped数组,但我的访问模式不是很规律,所以我希望memmapped数组更慢.以上似乎是第一个尝试的事情.这是在Linux上.我只是想从Stackoverflow获得一些建议,并且不想惹恼系统管理员.你认为它会帮助,如果第二个参数是像一个真正的不可变对象glbl_array.tostring().

pv.*_*pv. 108

您可以multiprocessing非常轻松地使用与Numpy一起使用的共享内存:

import multiprocessing
import ctypes
import numpy as np

shared_array_base = multiprocessing.Array(ctypes.c_double, 10*10)
shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
shared_array = shared_array.reshape(10, 10)

#-- edited 2015-05-01: the assert check below checks the wrong thing
#   with recent versions of Numpy/multiprocessing. That no copy is made
#   is indicated by the fact that the program prints the output shown below.
## No copy was made
##assert shared_array.base.base is shared_array_base.get_obj()

# Parallel processing
def my_func(i, def_param=shared_array):
    shared_array[i,:] = i

if __name__ == '__main__':
    pool = multiprocessing.Pool(processes=4)
    pool.map(my_func, range(10))

    print shared_array
Run Code Online (Sandbox Code Playgroud)

打印

[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
 [ 2.  2.  2.  2.  2.  2.  2.  2.  2.  2.]
 [ 3.  3.  3.  3.  3.  3.  3.  3.  3.  3.]
 [ 4.  4.  4.  4.  4.  4.  4.  4.  4.  4.]
 [ 5.  5.  5.  5.  5.  5.  5.  5.  5.  5.]
 [ 6.  6.  6.  6.  6.  6.  6.  6.  6.  6.]
 [ 7.  7.  7.  7.  7.  7.  7.  7.  7.  7.]
 [ 8.  8.  8.  8.  8.  8.  8.  8.  8.  8.]
 [ 9.  9.  9.  9.  9.  9.  9.  9.  9.  9.]]
Run Code Online (Sandbox Code Playgroud)

However, Linux has copy-on-write semantics on fork(),即使没有使用multiprocessing.Array,除非写入数据,否则不会复制数据.

  • 该副本仅复制refcount整数所在的内存页面.因此不会复制Numpy数组中的数据. (8认同)
  • 需要注意的是,Python fork()实际上意味着访问时的复制(因为只是访问该对象将改变其引用计数). (7认同)
  • 得到它了.你应该使用np.frombuffer(shared_array_base.get_obj())而不是np.ctypeslib.as_array (3认同)

Bri*_*ite 8

对于那些坚持使用不支持的 Windows fork()(除非使用 CygWin)的人,pv 的答案不起作用。全局变量不可用于子进程。

相反,您必须在Pool/的初始化过程中传递共享内存Process,如下所示:

#! /usr/bin/python

import time

from multiprocessing import Process, Queue, Array

def f(q,a):
    m = q.get()
    print m
    print a[0], a[1], a[2]
    m = q.get()
    print m
    print a[0], a[1], a[2]

if __name__ == '__main__':
    a = Array('B', (1, 2, 3), lock=False)
    q = Queue()
    p = Process(target=f, args=(q,a))
    p.start()
    q.put([1, 2, 3])
    time.sleep(1)
    a[0:3] = (4, 5, 6)
    q.put([4, 5, 6])
    p.join()
Run Code Online (Sandbox Code Playgroud)

(它不是麻木的,也不是好的代码,但它说明了这一点;-)


小智 7

以下代码可在Win7和Mac上运行(也许在Linux上,但未经测试)。

import multiprocessing
import ctypes
import numpy as np

#-- edited 2015-05-01: the assert check below checks the wrong thing
#   with recent versions of Numpy/multiprocessing. That no copy is made
#   is indicated by the fact that the program prints the output shown below.
## No copy was made
##assert shared_array.base.base is shared_array_base.get_obj()

shared_array = None

def init(shared_array_base):
    global shared_array
    shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
    shared_array = shared_array.reshape(10, 10)

# Parallel processing
def my_func(i):
    shared_array[i, :] = i

if __name__ == '__main__':
    shared_array_base = multiprocessing.Array(ctypes.c_double, 10*10)

    pool = multiprocessing.Pool(processes=4, initializer=init, initargs=(shared_array_base,))
    pool.map(my_func, range(10))

    shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
    shared_array = shared_array.reshape(10, 10)
    print shared_array
Run Code Online (Sandbox Code Playgroud)