python创建类对象数组_我可以在python中创建共享的多数组或列表对象列表以进行多处理吗?...

I need to make a shared object of a multidimensional array or list of lists for it to be available to the other processes. Is there a way to create it as for what i have seen it is not possible. I have tried:

from multiprocessing import Process, Value, Array

arr = Array('i', range(10))

arr[:]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

arr[2]=[12,43]

TypeError: an integer is required

I heard numpy array can be multiarray and a shared object, if above is not possible can someone tell me how to make a numpy array a shared object??

解决方案

To make a numpy array a shared object (full example):

import ctypes as c

import numpy as np

import multiprocessing as mp

n, m = 2, 3

mp_arr = mp.Array(c.c_double, n*m) # shared, can be used from multiple processes

# then in each new process create a new numpy array using:

arr = np.frombuffer(mp_arr.get_obj()) # mp_arr and arr share the same memory

# make it two-dimensional

b = arr.reshape((n,m)) # b and arr share the same memory

If you don't need a shared (as in "share the same memory") object and a mere object that can be used from multiple processes is enough then you could use multiprocessing.Manager:

from multiprocessing import Process, Manager

def f(L):

row = L[0] # take the 1st row

row.append(10) # change it

L[0] = row #NOTE: important: copy the row back (otherwise parent

#process won't see the changes)

if __name__ == '__main__':

manager = Manager()

lst = manager.list()

lst.append([1])

lst.append([2, 3])

print(lst) # before: [[1], [2, 3]]

p = Process(target=f, args=(lst,))

p.start()

p.join()

print(lst) # after: [[1, 10], [2, 3]]

Server process managers are more flexible than using shared memory

objects because they can be made to support arbitrary object types.

Also, a single manager can be shared by processes on different

computers over a network. They are, however, slower than using shared

memory.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值