相关疑难解决方法(0)

在共享内存中使用numpy数组进行多处理

我想在共享内存中使用numpy数组与多处理模块一起使用.困难是使用它像一个numpy数组,而不仅仅是一个ctypes数组.

from multiprocessing import Process, Array
import scipy

def f(a):
    a[0] = -a[0]

if __name__ == '__main__':
    # Create the array
    N = int(10)
    unshared_arr = scipy.rand(N)
    arr = Array('d', unshared_arr)
    print "Originally, the first two elements of arr = %s"%(arr[:2])

    # Create, start, and finish the child processes
    p = Process(target=f, args=(arr,))
    p.start()
    p.join()

    # Printing out the changed values
    print "Now, the first two elements of arr = %s"%arr[:2]
Run Code Online (Sandbox Code Playgroud)

这会产生如下输出:

Originally, the first two elements of arr = …
Run Code Online (Sandbox Code Playgroud)

python shared numpy multiprocessing

95
推荐指数
6
解决办法
6万
查看次数

标签 统计

multiprocessing ×1

numpy ×1

python ×1

shared ×1