python 共享内存容量,如何在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())

解决方案

try :

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值