如何在Python中调整共享内存的大小 [英] How to resize a shared memory in Python

查看:265
本文介绍了如何在Python中调整共享内存的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一个数组作为共享内存.问题是程序的结构是这样的:在我知道共享数组的大小之前就生成了子进程.如果我发送消息扩展数组,则什么也不会发生,如果我尝试发送共享数组本身,则会收到错误消息.下面是一个演示我的问题的小脚本.

I want to use an array for shared memory. The problem is the program is structured in such a way that the child processes are spawned before I know the size of the shared array. If I send a message to extend the array nothing happens and if I try to send the shared array itself I get an error. Below is a small script to demonstrate my problem.

import multiprocessing as mp 
import numpy as np

def f(a,pipe):
    while True:
        message, data = pipe.recv()
        if message == 'extend':
            a = np.zeros(data)
            print a
        elif message == 'exit':
            break


if __name__ == '__main__':

    unshared_arr = np.zeros(1)
    a = mp.Array('d', unshared_arr)

    p1,p2 = mp.Pipe()

    p = mp.Process(target=f, args=(a,p2))
    p.start()


    p1.send(('extend', 10))

    p1.send(('exit', None))

    p.join()

    b = np.frombuffer(a.get_obj())

推荐答案

尝试:

unshared_Arr = mp.Array(ctypes.c_uint8,SIZE_NEEDED) #should be size 
                                                    #and not the array itself
np_shared = np.frombuffer(ushared_Arr.get_obj(),dtype=ctypes.c_uint8)
np_shared.reshape(SIZE_NEEDED/2,SIZE_NEEDED/2)  #or (,SIZE_NEEDED) ie. any shape 
                                                #you want as long as the allocated size 
                                                #does not change

现在像使用任何numpy数组一样使用np_shared.如果需要多个进程,则应将其设置为全局.

now use np_shared as you would any numpy array. You should keep it global if multiple processes are going to need it.

这篇关于如何在Python中调整共享内存的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆