-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
26 lines (20 loc) · 766 Bytes
/
memory.py
File metadata and controls
26 lines (20 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Wraps a numpy array in shared memory so it can be accessed across sub-interpreters
import numpy as np
from multiprocessing.shared_memory import SharedMemory
class SharedArray:
def __init__(self, array):
self._shm = SharedMemory(create = True, size = array.nbytes)
self._shared_array = np.ndarray(array.shape, dtype = array.dtype, buffer = self._shm.buf)
self._shared_array[:] = array[:]
self._shape = array.shape
self._dtype = array.dtype
def get(self):
return self._shared_array
def close(self):
self._shm.close()
self._shm.unlink()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
return None