2016-08-04 18 views
1

Ich habe ein großes Array (~ 500k Zeilen x 9 Spalten), die ich teilen möchte, wenn eine Reihe von parallelen Prozessen mit Python multiprocessing-Modul ausgeführt wird. Ich verwende this SO Antwort, um mein freigegebenes Array zu erstellen, und ich verstehe von this SO beantworten, dass das Array gesperrt ist. Aber in meinem Fall, da ich nie gleichzeitig in dieselbe Zeile schreibe, ist eine Sperre überflüssig und erhöht die Verarbeitungszeit.Freigeben eines Ctyps Numpy-Array ohne Sperre bei Verwendung von Multiprocessing

Wenn ich lock=False angeben, erhalte ich einen Fehler.

Mein Code ist dies:

shared_array_base = multiprocessing.Array(ctypes.c_double, 90, lock=False) 
shared_array = np.ctypeslib.as_array(shared_array_base.get_obj()) 
shared_array = shared_array.reshape(-1, 9) 

und der Fehler ist dies:

--------------------------------------------------------------------------- 
AttributeError       Traceback (most recent call last) 
<ipython-input-15-d89681d70c37> in <module>() 
     1 shared_array_base = multiprocessing.Array(ctypes.c_double, len(np.unique(value)) * 9, lock=False) 
----> 2 shared_array = np.ctypeslib.as_array(shared_array_base.get_obj()) 
     3 shared_array = shared_array.reshape(-1, 9) 

AttributeError: 'c_double_Array_4314834' object has no attribute 'get_obj' 

Meine Frage ist, wie kann ich eine numpy Array teilen, die nicht jedes Mal, wenn ich es schreiben gesperrt ist?

Antwort

1

die Antwort gefunden here dank HYRY

lock=True Angabe liefert ein verpacktes Objekt:

multiprocessing.sharedctypes.SynchronizedArray 

Wenn lock=False eine rohe Array zurückgibt, die nicht die .get_obj() Methode

multiprocessing.sharedctypes.c_double_Array_10 

Daher hat Code zum Erstellen eines entsperrten Array ist dies:

shared_array_base = multiprocessing.Array(ctypes.c_double, 90, lock=False) 
shared_array = np.ctypeslib.as_array(shared_array_base) 
shared_array = shared_array.reshape(-1, 9) 
+0

Sie könnten auch versuchen, shared_array = np.frombuffer (shared_array_base) .reshape (-1, 9) ' –