您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

要使numpy数组成为共享对象(完整示例):

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

如果您不需要 共享的对象 (如“共享相同的内存”),并且可以在多个进程中使用的对象就足够了,则可以使用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]]

文档

服务器进程管理器比使用共享内存对象更灵活,因为可以使它们支持任意对象类型。同样,可以通过网络上不同计算机上的进程共享一个管理器。但是,它们比使用共享内存慢。

python 2022/1/1 18:35:10 有217人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶