問題描述
我需要創建一個多維數組或列表列表的共享對象,以便其他進程可以使用它.有沒有辦法創建它,因為我已經看到它是不可能的.我試過了:
from multiprocessing import Process, Value, Arrayarr = Array('i', range(10))arr[:][0, 1, 2, 3, 4, 5, 6, 7, 8, 9]arr[2]=[12,43]TypeError:需要一個整數
我聽說 numpy 數組可以是多數組和共享對象,如果以上不可能,有人可以告訴我如何使 numpy 數組成為共享對象嗎??
將 numpy 數組變成共享對象(完整示例):
將 ctypes 導入為 c將 numpy 導入為 np將多處理導入為 mpn, m = 2, 3mp_arr = mp.Array(c.c_double, n*m) # 共享,可以從多個進程中使用# 然后在每個新進程中創建一個新的 numpy 數組,使用:arr = np.frombuffer(mp_arr.get_obj()) # mp_arr 和 arr 共享同一個內存# 讓它變成二維的b = arr.reshape((n,m)) # b 和 arr 共享同一個內存
如果您不需要 shared (如共享同一內存")對象并且僅可以從多個進程中使用的對象就足夠了,那么您可以使用 multiprocessing.經理
:
from multiprocessing import Process, Manager定義 f(L):row = L[0] # 取第一行row.append(10) # 改變它L[0] = row #注意:重要:將行復制回來(否則為父#process 不會看到更改)如果 __name__ == '__main__':經理=經理()lst = manager.list()lst.append([1])lst.append([2, 3])print(lst) # before: [[1], [2, 3]]p = 進程(目標=f,args=(lst,))p.start()p.join()print(lst) # after: [[1, 10], [2, 3]]
來自文檔:p><塊引用>
服務器進程管理器比使用共享內存更靈活對象,因為它們可以支持任意對象類型.此外,單個管理器可以由不同的進程共享通過網絡的計算機.但是,它們比使用共享的要慢記憶.
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]]
From the docs:
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.
這篇關于我可以在 python 中創建共享的多數組或列表對象列表以進行多處理嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!