Shared numpy ndarray with serialization interface. This class can be used as a drop-in replacement for arguments in multiprocessing. Parameters: array (array-like): input data
| 85 | |
| 86 | |
| 87 | class SharedNDArray(np.memmap): |
| 88 | """ |
| 89 | Shared numpy ndarray with serialization interface. |
| 90 | This class can be used as a drop-in replacement for arguments in multiprocessing. |
| 91 | |
| 92 | Parameters: |
| 93 | array (array-like): input data |
| 94 | """ |
| 95 | def __new__(cls, array): |
| 96 | if "linux" not in sys.platform: |
| 97 | raise EnvironmentError("SharedNDArray only works on Linux") |
| 98 | |
| 99 | array = np.asarray(array) |
| 100 | file = tempfile.NamedTemporaryFile() |
| 101 | self = super(SharedNDArray, cls).__new__(cls, file, dtype=array.dtype, shape=array.shape) |
| 102 | # keep reference to the tmp file, otherwise it will be released |
| 103 | self.file = file |
| 104 | self[:] = array |
| 105 | return self |
| 106 | |
| 107 | @classmethod |
| 108 | def from_memmap(cls, *args, **kwargs): |
| 109 | return super(SharedNDArray, cls).__new__(cls, *args, **kwargs) |
| 110 | |
| 111 | def __reduce__(self): |
| 112 | order = "C" if self.flags["C_CONTIGUOUS"] else "F" |
| 113 | return self.__class__.from_memmap, (self.filename, self.dtype, self.mode, self.offset, self.shape, order) |
| 114 | |
| 115 | def __array_wrap__(self, arr, context=None): |
| 116 | arr = super(np.memmap, self).__array_wrap__(arr, context) |
| 117 | |
| 118 | if self is arr or type(self) is not SharedNDArray: |
| 119 | return arr |
| 120 | if arr.shape == (): |
| 121 | return arr[()] |
| 122 | |
| 123 | return arr.view(np.ndarray) |
| 124 | |
| 125 | |
| 126 | class Monitor(object): |
no outgoing calls
no test coverage detected