找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 5934|回复: 0

麻烦python高手给看下多进程怎么共享numpy的array

1

主题

1

帖子

1

积分

贫民

积分
1
saturdays 发表于 2018-5-3 12:27:54 | 显示全部楼层 |阅读模式
我为了共享array,把他的data buffer拿出来给了子进程。
  
shared_batch = np.empty([32,3,224,224], dtype=np.float32)
buf_ptr = shared_batch.ctypes.data
print 'init address ', buf_ptr  
processes = [Process(target=sub_process,args=(shared_batch.data) ) for i in range(4)]
  
然后子进程里
def read_images(shared_buffer):
     with lock:
         buf_ptr = buf.ctypes.data
         print 'befrore set %d, %f'%(buf_ptr, buf[0])
         buf = np.frombuffer(shared_buffer, dtype=np.float32, count=3*224*224, offset=0)
         buf_ptr = buf.ctypes.data
         print 'befrore set %d, %f'%(buf_ptr, buf[0])
         buf[0] = 0.35
子进程结束后,到主进程去
print shared_batch[0]
  
死活不是0.35,一直是初始值。所有的地址buf_ptr,都是一模一样的。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表